常量池c语言,基本类型包装类及编译器优化(常量池)

1.Boolean

/**

* @author Aruforce

* Booelean 为不可变类,Java实现了 常量池机制。

* 直接赋值'public static Boolean flag1 = true;' 会被优化成

* 'public static Boolean flag1 = Boolean.valueOf(true);'返回一个Boolean的固定地址

*/

public class TestBoolean {

public static Boolean flag1 = true;//常量池

public static Boolean flag2 = true;//常量池

public static void main(String[] args) {

System.out.println(flag1==flag2);//true

Boolean flag3 = new Boolean(true);//堆内的对象

System.out.println(flag1 == flag3);//false

Boolean flag4 =true;//常量池

System.out.println(flag1 == flag4);//true

flag4 = new Boolean(false);//堆内的对象

System.out.println(flag3 == flag4);//false

}

}

2.Byte

/**

*

* @author Aruforce

* Byte为不可变类,Java为其实现了常量池机制

* 直接赋值'public static Byte b1 = 1' -128-0-127 会被优化成

* 'Byte.valueOf(1)' 返回ByteCache.cache[]里面对应的值

*/

public class TestByte {

public static Byte b1 = 1;//常量池

public static Byte b2 = 1;//常量池

public static void main(String[] args) {

Byte b3 = 1;

Byte b4 = new Byte((byte)1);

System.out.println(b1 == b2);//true

System.out.println(b1 ==b3);//true

System.out.println(b1==b4);//false

}

}

3.Charactor

/**

*

* @author Aruforce

* Character 为不可变类,Java为(Character c int值在127以下时)实现了常量池机制

* 直接赋值'public static Character character1 = 'A' 被编译器优化为

* 'public static Character character1 = Character.valueOf('A')'

*

*/

public class TestCharactor {

public static Character character1 = 'A';

public static Character character2 = 'A';

public static Character character11 = '中';

public static void main(String[] args) {

Character character3 = 'A';

Character character4 = new Character('A');

System.out.println(character1 == character2);//true

System.out.println(character1 == character3);//true

System.out.println(character1 == character4);//false

Character character12 = '中';

Character character13 = new Character('中');

System.out.println(character11 == character12);//false

System.out.println(character12 == character13);//false

}

}

4.Double

/**

*

* @author Aruforce

* Double 是不可变类,但是Java未实现常量池机制

* public static Double d1 = 1.0 优化为 public static Double d1 = Double.valueOf(1.0D);

* 实际上也是调用 new Doubel();

*/

public class TestDouble {

public static Double d1 = 1.0;

public static Double d2 = 1.0;

public static void main(String[] args) {

Double d3 = 1.0;

Double d4 = new Double(1.0);

System.out.println(d1 == d2);//false

System.out.println(d1 == d3);//false

System.out.println(d1 == d4);//false

}

}

5.Float

/**

*

* @author Aruforce

* Float 是不可变类,但是Java未实现常量池机制,显然数值类型的也不需要

* public static Float f1 = 1.0f; 优化为 public static Float f1 = Float .valueOf(1.0f);

* 实际上也是调用 new Float();

*/

public class TestFloat {

public static Float f1 = 1.0f;

public static Float f2 = 1.0f;

public static void main(String[] args) {

Float f3 = 1.0f;

Float f4 = new Float(1.0f);

System.out.println(f1 == f2);//false

System.out.println(f1 == f3);//false

System.out.println(f1 == f4);//false

}

}

6.Integer

/**

*

* @author Aruforce

* {@link Integer} 是不可变类,Java为介于-128-0-127的实现了常量池

* public static Integer i1 = 127;被编译器优化为

* public static Integer i1 = Integer.valueOf(1);

*/

public class TestInteger {

public static Integer i1 = 127;

public static Integer i2 = 127;

public static void main(String[] args) {

Integer i3 = 127;

Integer i4 = new Integer(127);

System.out.println(i1 == i2);// true

System.out.println(i1 == i3);// true

System.out.println(i1 == i4);// false

}

}

7.Long

/**

*

* @author Aruforce

* {@link Long} 是不可变类,Java为介于-128-0-127的实现了常量池

* public static Long i1 = 127;被编译器优化为

* public static Long i1 = Long.valueOf(1);

*/

public class TestLong {

public static Long l1 = 127l;

public static Long l2 = 127l;

public static void main(String[] args) {

Long l3 = 127l;

Long l4 = new Long(127l);

System.out.println(l1 == l2);// true

System.out.println(l1 == l3);// true

System.out.println(l1 == l4);// false

}

}

8.Short

/**

*

* @author Aruforce

* {@link Short} 是不可变类,Java为介于-128-0-127的实现了常量池

* public static Short i1 = 127;被编译器优化为

* public static Short i1 = Short.valueOf(1);

*/

public class TestShort {

public static Short s1 = 127;

public static Short s2 = 127;

public static void main(String[] args) {

Short s3 = 127;

Short s4 = new Short((short)127);

System.out.println(s1 == s2);// true

System.out.println(s1 == s3);// true

System.out.println(s1 == s4);// false

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值