java父类,子类静态块和构造方法的执行顺序

一个面试笔试题中没什么什么卵用但经常出的题,父类,子类静态块和构造方法的执行顺序

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
 

转载于:https://my.oschina.net/chendongj/blog/1575069

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值