日期:2014-05-18  浏览次数:20713 次

【更新】 对java中一些关键的,容易迷惑的知识点的归纳总结, 请朋友们指正,完善, 一同交流 ,我会持续更新
近段强化了java中的一些知识点(会涉及很多),明朗许多,现总结如下,一方面供朋友们参考,另一方面里面不足,错误的,希望朋友们指正完善善:

一. Switch[u][/u]
  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[u][/u]
  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”); [u][/u]
  该语句会产生2个字符串对象:
一个是通过 ” ” 方式在 编译期 产生,存放在常量池中
一个是通过new方式在 运行期 产生,存放在堆内存中
但在运行时只会通过new方式产生一个对象

四. java中的参数只能“按値”传递,且传递的是値的 copy[u][/u]
  如是基本类型,则传递的是基本类型的副本
如是引用类型,则传递的是引用本身的副本
  参见2的实例

五. 方法重载和覆盖的条件[u][/u]
符合重载的条件: 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;)