一个面试笔试题中没什么什么卵用但经常出的题,父类,子类静态块和构造方法的执行顺序
package com.gulf.test;
public class Father {
public Father(){
System.out.println("父类构造方法");
}
static {
System.out.println("父类静态块");
}
}
package com.gulf.test;
public class Son extends Father {
public Son(){
System.out.println("子类构造方法");
}
static{
System.out.println("子类静态块");
}
public static void main(String[] args) {
new Son();
System.out.println("---第二次new对象---");
new Son();
}
}
执行结果:
父类静态块
子类静态块
父类构造方法
子类构造方法
---第二次new对象---
父类构造方法
子类构造方法
package com.gulf.test;
public class StringTest {
public static void main(String[] args) {
String a = "123";
String b = new String("123");
String c = "12"+"3";
System.out.println(a==b);
System.out.println(a==c);
System.out.println(b==c);
System.out.println(b.equals(a));
}
}
没用的字符串比较输出结果:
false
true
false
true
package com.gulf.test;
public class IntegerTest {
public static void main(String[] args) {
int i = 1;
Integer j = 1;
System.out.println(i == j);
Integer m = 127;
Integer n = 127;
System.out.println(m == n);
Integer k = 128;
Integer h = 128;
System.out.println(k == h);
int x = 128;
System.out.println(k == x);
}
}
没用的int和integer比较
输出结果:
true
true
false
true