对java中的一些关键的,容易迷惑的知识点总结归纳,请朋友们指正,一同完善, 我会持续更新
近段强化了java中的一些知识点,明朗了许多,现总结如下,供一方面朋友们参考,另一方面里面不足的,错误的,也请朋友们指正完善:
1. Switch
a. 其能接受的数据类型有四个,char , byte, short, int
b. Default 可放在switch中的任何一个地方,但只有给定的条件匹配不到时,才会执行
c. Case,default语句如果执行完要跳出,必须用break, 没的话会向下继续执行(如果碰到case语句则直接进入执行)
实例:
1.int i=1, j=0
2.
3.switch(i) {
4.case 2:
5.j+=6;
6.
7.case 4:
8.j+=1;
9.
10.default:
11.j +=2;
12.
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
103.Given:
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
2. 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 text BufferString = new StringBuffer (“java”);
13.
14. stringReplace (textString);
15. BufferReplace (textBuffer);
16.
17. System.out.printIn (textString + textBuffer);
18. }
19. }
What is the output?
Ans:
3. String s = new String(“XYZ”);
该语句会产生2个字符串对象,但在运行时只会产生一个对象
一个是通过 ” ” 方式在 编译期 产生,存放在常量池中
一个是通过new方式在 运行期 产生,存放在堆内存中
4. java中的参数只能“按値”传递,且传递的是値的 copy
如是基本类型,则传递的是基本类型的副本;
如是引用类型,则传递的是引用本身的副本
实例参见3的实例
5. 方法重载和覆盖的条件
符合重载的条件:
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;}
6. 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. 数组的初始化
数组在分配空间时就开始了初始化,
实例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 Parent1 {
// 静态变量
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 Parent1() {
System.out.println("父类--构造器");
}
}
public class initialSubClass extends Parent1 {
// 静态变量
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 initialSubClass() {
System.out.println("子类--构造器");
}
// 程序入口
public static void main(String[] args) {
new initialSubClass();
}
}