对java中一些关键的,容易迷惑的知识点的归纳总结

来源:互联网 发布:马歇尔经济学原理 知乎 编辑:程序博客网 时间:2024/05/16 08:53
 一.  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;}

100  修改删除举报引用回复

进入用户个人空间
加为好友
发送私信
在线聊天
  • myJavaRoad
  • 等级:
  • 可用分等级:
  • 总技术分:
  • 总技术分排名:
发表于:2009-03-27 14:54:371楼 得分:0调整了半天还是不理想,明明发之前调整好了,一发就变了样

六.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


修改删除举报引用回复

进入用户个人空间
加为好友
发送私信
在线聊天
  • myJavaRoad
  • 等级:
  • 可用分等级:
  • 总技术分:
  • 总技术分排名:
发表于:2009-03-27 15:45:032楼 得分:0
十.for(;;)意义
相当于while(true), 不知道java为什么要搞出这个古怪让人费解的东西?

十一.equals, = =
  equals比较两个对象的内容是否相等
    = = 比较的是两个引用是否指向同一对象
    String的存储特性会对以上的判定规则产生影响:
  String 通过“”生成的对象会保存在常量池中,常量池有一个很重要的特点就是能共享,比如String s = “X”;在把”X”放常量池之前jvm会检测常量池中是否存在相同的对象,如果已经存在则直接把引用指向已存在的对象,不再为”X”分配空间,好处是节约了空间
实例:
  String s1 = “ABC”;
  String s2 = “ABC”;
  以下各结果为true, 还是为false
  S1 == s2;  //true ,String的特性决定的
  S1.equals(s2);  //true

Jdk1.5后引入了自动打包自动解包的功能,对以上的判定规则也会产生影响:比如以下是正确的定义
  Double d = 1.0;  //java编译器会自动把1.0打包成New Double(1.0);

实例:
Integer i = new Integer (42);
Long l  = new Long (42);
Double d = new Double (42.0);

Which one expressions evaluate to True?
A.(i == l)
B.(i == d)
C.(d == l)
D.(i.equals (d))
E.(d.equals (i))
F.(i.equals (42))  //42会自动打包成new Integer(42)

十二. 基本类型的变量赋初始値
  Byte的范围为-128~127
  当我们给出一个整数,且该整数后不带l标示,则编译器自动把它视为int类型,如
      Int i = 1 ; 是成立的
  当我们给出一个小数,且该小数后不带f标示,则编译器自动把它视为double类型,如
      Double d = 1.0; 是成立的


十三. 基本类型的转化
  规则: 小的可以自动转化为大的, 大的要强制性才能转为小的,比如以下
      Double d = 1.0f;  //正确, 小转大,自动
      Float f  = 1.0d(或1.0);  //错误,大转小,需强制 float f = (float)1.0d;
原创粉丝点击