main
2018-12-25 本文已影响0人
阿桃_28e7
/**测试null Object直接强转Integer, 强转成功,输出null*/
public static void main(String[] args) {
Object data = null;
Integer data1 = (Integer) data;
System.out.println(data1);
}
/**验证 null+"" 变成字符串"null",确实如此*/
public static void main(String[] args) {
Integer num = null;
String numStr = num + "";
System.out.println(null == numStr);
System.out.println("null".equalsIgnoreCase(numStr));
}
/**验证String和Boolean对象相等问题,Boolean很特殊*/
public static void main(String[] args) {
String s1 = new String("newString");
String s2 = new String("newString");
String s3 = "string";
String s4 = "string";
System.out.println(s1 == s2);//false
System.out.println(s3 == s4);//true
Boolean b1 = new Boolean("true");
Boolean b2 = new Boolean("true");
Boolean b3 = Boolean.valueOf("true");
Boolean b4 = Boolean.valueOf("true");
System.out.println(b1.equals(b2));//true
System.out.println(b3.equals(b4));//true
}
/**测试自动装箱带来的性能问题*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
/**使用Long耗时9秒,自动装箱导致构造了大约2^31个多余Long实例*/
Long sum = 0L;
/**使用long耗时0秒*/
// long sum = 0L;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
sum += i;
}
long endTime = System.currentTimeMillis();
System.out.println("总和: " + sum + "\n耗时: " + (endTime-startTime)/1000);
}
/**instanceOf 和 getClass*/
class Super{}
class Sub extends Super{
public static void main(String[] args) {
Super parent = new Super();
Sub child = new Sub();
System.out.println(child instanceof Super);
System.out.println(child.getClass() == parent.getClass());
}
}