当前位置: 首页 > 图文教程 > Java技术 > Java基础 > java中容易迷惑的知识点

Java基础
apatche组件的研究和使用
定时执行任务的三种方法
Java各种路径和参数
我看如何选择java和.net
Java关闭窗体的六种方法
java中容易迷惑的知识点
一个简单Thread缓冲池的实现
Java Swing中键盘事件的处理
解读内存优化编程
Java中Set的深入研究
一套JDOM操作XML文件的Base Class
Java堆和栈的区别
Thread类和Runable接口
java 定时执行任务,java定时器
递归(recursion)
java访问各种数据库连接代码
也谈JAVA值传参和引用传参
(转)一个牛人给java初学者的建议
java gui学习
JNI中文处理问题小结

Java基础 中的 java中容易迷惑的知识点


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 92 ::
收藏到网摘: n/a

一.  Switch
      1.其能接受的数据类型有四个,char , byte, short, int
      2.Default 可放在switch中的任何一个地方,但只有给定的条件匹配不到时,才会执行
        3.Case,default语句如果执行完要跳出,必须用break,  没的话会向下继续执行(如果碰到case语句则直接进入执行)

实例1:
1.int i=1, j=0 
3.switch(i){
4.    case 2:
5.   j+=6;
7.    case 4:
8.   j+=1;
10.  default:
11.   j +=2;
13.  case 0:
14.   j +=4;
15.} 

What is the value of j at line 16?
A.0
B.1
C.2
D.4
E.6

实例2:
1. switch (i) {
2. default:
3. System.out.printIn(“Hello”);
4. }

What is the acceptable type for the variable i?
A.byte
B.long
C.float
D.double
E.object
F.A and B
G.C and D 

二.  String 和 StringBuffer
    String 定义的是字符串常量,其値一旦定义就不再改变,如下:
        String s  =  “ABC”;
        S  =  s.subString(2); //会重新生成一个字符串对象
        以上两句执行后在内存中会产生“两”个字符串对象 一个”ABC”,另一个是s指向的”AB”(注意s已不再指向”ABC”)
    StringBuffer 定义的是字符串变量,其値可以改变,如下:
        StringBuffer s1  =  new StringBuffer(“ABC”);
        S1 =  s1.subString(2);
        以上两句执行后在内存中只产生“一个”字符串对象: s指向的”AB”; 

实例1:
1.public class Foo {
2.  public static void main (String [] args){
3.    StringBuffer a = new StringBuffer (“A”);
4.    StringBuffer b = new StringBuffer (“B”);
5.    operate (a,b);
6.    system.out.printIn{a + “,” +b};
7.}
8.  static void operate (StringBuffer x, StringBuffer y)  {
9.          x.append {y};
10.          y = x;
11.    )
12.}
What is the output?
Ans:

实例2:
1.Public class test{
2.Public static void stringReplace (String text){
3.    Text = text.replace (‘j’ , ‘i’);
4.}
5.
6.public static void bufferReplace (StringBuffer text) {
7.    text = text.append (“C”)
8.}
9.
10.public static void main (String args[])  {
11.  String textString = new String (“java”);
12.  StringBuffer textBuffer = new StringBuffer (“java”);
13.
14.    stringReplace (textString);
15.    BufferReplace (textBuffer);
16.
17.    System.out.printIn (textString + textBuffer);
18.    }
19. }

What is the output?
Ans:

三.  String s = new String(“XYZ”);
    该语句会产生2个字符串对象:
    一个是通过 ” ” 方式在 编译期 产生,存放在常量池中
    一个是通过new方式在 运行期 产生,存放在堆内存中
    但在运行时只会通过new方式产生一个对象


四.  java中的参数只能“按値”传递,且传递的是値的 copy
  如是基本类型,则传递的是基本类型的副本
    如是引用类型,则传递的是引用本身的副本

    参见2的实例

五.  方法重载和覆盖的条件
符合重载的条件: 1.在同一个类中
                2.有多个同名的方法,
                3.方法参数不同(参数的个数不同 或则 参数的类型不同)

实例:
1.public class MethodOver  {
2.    public void setVar (int a, int b, float c)  {
3.    }
4.}

Which two overload the setVar method? (Choose Two)

A.private void setVar (int a, float c, int b)  { }
B.protected void setVar (int a, int b, float c) { }
C.public int setVar (int a, float c, int b) (return a;)
D.public int setVar (int a, int b, float c) (return a;)
E.protected float setVar (int a, int b, float c) (return c;)


符合覆盖的条件:  1.在继承中
                  2.子类中的方法名和父类相同
                  3.子类中的方法参数和父类相同
                  4.子类中的方法返回类型和父类一样
                  5.子类的方法不能比父类抛出更多的异常
                  6.子类的方法访问范围大于或等于父类

  覆盖值得注意的是如果子类中有一个方法名称和父类一样,但参数不同,那不叫覆盖,所以也就不受覆盖的条件限制(注意该方法可以存在)
实例:
          1.class BaseClass {
2.  Private float x = 1.0f ;
3.    protected float getVar ( ) ( return x;)
4.}
5.class Subclass extends BaseClass (
6.      private float x = 2.0f;
7.      //insert code here
8.)

Which two are valid examples of method overriding? (Choose Two)
A.float getVar ( ) { return x;}
B.public float getVar ( ) { return x;}
C.float double getVar ( ) { return x;}
D.protected float getVar ( ) { return x;}
E.public float getVar (float f ) { return f;}


六. java类中的变量初始化相关的知识:6-1.初始化顺序分三步:
1. 类加载时,初始化静态变量和静态区块,先父类后子类
2. 运行中当new出一个对象时,开始为对象分配空间并初始化实例变量,先父类后子类
3. 调用构造函数时,先执行父类的构造函数,再执行子类的构造函数,具体过程是调用子类的构造函数时,在第一行处会调用父类的构造函数(显式或隐式)

6-2. 初始化时各类型的变量初始化的値:
应用类型: null
基本类型: boolean : false
          Char:\u0000
          Byte: 0
          Short: 0
          Int: 0
          Long: 0
          Float: 0.0
          Double: 0.0

    6-3. 数组的初始化
          当我们产生某个存储对象的数组时,真正产生的其实是个存储references的数组。此数组建立之后,其中的每一个reference皆会被自动设为某个特殊值。该值以关键字null表示。当Java看到null值,便将这个reference视为“不指向任何对象”。使用任何reference之前,你必须先将某个对象指派给它。如果你使用某个reference而其值为null,便会在执行期发生错误
        数组在分配空间时就开始了初始化,初始化规则,基本类型按照6-2的规则进行初始化,应用类型类型全部初始化为null

实例1 :
int index = 1;
int [] foo = new int [3];
int bar = foo [index];
int baz = bar + index;

What is the result?
A.baz has the value of 0
B.baz has the value of 1
C.baz has the value of 2
D.an exception is thrown
E.the code will not compile

实例2:
1.String foo = “blue”;
2.Boolean[]bar = new Boolean [1];
3.if (bar[0]) {
4.  foo = “green”;
5.}

What is the result?

A.foo has the value of “”
B.foo has the value of null
C.foo has the value of “blue”
D.foo has the value of “green”
E.an exception is thrown
F.the code will not compile

      6-4. java中的所有的实例变量都有系统默认初始化,所有的方法变量由方法本身进行初始化,且方法中的变量一定要初始化后才能应用
例题:
  class Parent {       
    // 静态变量       
    public static String p_StaticField = "父类--静态变量";       
    // 变量       
    public String p_Field = "父类--变量";       
     
    // 静态初始化块       
    static {       
        System.out.println(p_StaticField);       
        System.out.println("父类--静态初始化块");       
    }       
     
    // 初始化块       
    {       
        System.out.println(p_Field);       
        System.out.println("父类--初始化块");       
    }       
     
    // 构造器       
    public Parent() {       
        System.out.println("父类--构造器");       
    }       
}       
     
public class SubClass extends Parent {       
    // 静态变量       
    public static String s_StaticField = "子类--静态变量";       
    // 变量       
    public String s_Field = "子类--变量";       
    // 静态初始化块       
    static {       
        System.out.println(s_StaticField);       
        System.out.println("子类--静态初始化块");       
    }       
    // 初始化块       
    {       
        System.out.println(s_Field);       
        System.out.println("子类--初始化块");       
    }       
     
    // 构造器       
    public SubClass() {       
        System.out.println("子类--构造器");       
    }       
     
    // 程序入口       
    public static void main(String[] args) {       
        new SubClass();       
    }       

 
七. java中的构造函数
1. 构造函数不能被继承
2. 每一个类都至少有一个构造函数,自己不定义,编译器也会给分配一个默认的不带参数的构造函数
3. 子类的构造函数一定会调用父类的构造函数,通过super()调用,或显式或隐式,显式调用的父类构造函数必须存在; 如果没有显式调用则编译器会自动在子类的构造函数第一行处加上super()这个隐式调用,这时要求父类一定要有不带参数的构造函数存在(如果父类自己定义了构造函数,但带有参数,编译时会报错)

例子:
class super1{
public int I = 0;
public super1 (String text){
I = 1;
}
}
public class sub1 extends super1{
public sub1(String text){
  // super(text);
I= 2;
//隐式超级构造super1()是未定义的。必须明确援引另一个构造
}
public static void main (String args[]){
sub1 sub2 = new sub1("Hello");
System.out.println(sub2.I);
}
}

八. java中的异常处理
1. java中的异常分运行时异常 和 非运行时异常, 运行时异常由运行时系统捕获并处理(编译正常),非运行时异常必须由处理(抛出或捕获)

2. 异常机制中try{}后一定要跟catch吗?
* 不一定,,但必须跟finally.也就是catch和finally必须跟其中一个
* 异常机制中try{}后一定要跟catch吗?
* 不一定,,但必须跟finally.也就是catch和finally必须跟其中一个
*  try {     
*  }finally {}
* 这样没问题,而且,可不是没有意义哦,因为这样可以保证即使发生了异常,finally里面的代码一定会被执行。
* 有时候,这个还是非常有用的。
* 比如可以用来释放一些自己占用的资源,然后让调用者处理异常。
  3.  异常中的finally一定会执行,哪怕一个方法中有return语句,也是在异常处理后才返回
  4.  异常的抛出可以先子类再父类,如果子类捕获了,则父类就不再捕获;
但是不能先父类再子类,那样会导致编译出错
  5.  异常处理后,程序继续执行

实例:
/*
* 非运行时异常一旦抛出,要么用catch块捕获处理,要么声明抛出
*/
import java.io.IOException;
public class ExceptionTest{
//public static void methodA(){
public static void methodA() throws IOException{
//throw new NullPointerException();
//try{
throw new IOException();
//System.out.println("method exit");
//}catch(IOException e){}
//finally{}
}
public static void main (String[] args){
try {
methodA();
//throw new IOException();
} catch (IOException e)  { System.out.println("Caught1 IOException ");
} catch (NullPointerException e)  {
System.out.println("Caught1 NullPointerException");
} catch (Exception e)  {
System.out.println("Caught Exception");
}

System.out.println("main exit");
}
}

What is the output?
Ans:


九. 按位运算和逻辑运算
    按位运算操作符(& ,| )两边的都要计算
    逻辑运算如果操作符(&&, || )左边成立则就不在计算右边了


实例:
1.public class test{   
2.    private static int j = 0;     
4.    private static boolean methodB(int k) {
5.        j += k;
6.        return true;
7.}
9.    public static void methodA(int  i) {
10.        boolean b: 
11.        b = i < 10 | methodB (4);
12.        b = i < 10 || methodB (8);
15.    public static void main (String args[] ) {
16.        methodA (0);
17.        System.out.println(j);
18.  }
19}

What is the result?
A.The program prints “0”
B.The program prints “4”
C.The program prints “8”
D.The program prints “12”
E.The code does not complete