/**
*
* @author Aruforce
* Byte為不可變類鸟蟹,Java為其實現(xiàn)了常量池機制
* 直接賦值'public static Byte b1 = 1' -128-0-127 會被優(yōu)化成
* 'Byte.valueOf(1)' 返回ByteCache.cache[]里面對應(yīng)的值
*/
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以下時)實現(xiàn)了常量池機制
* 直接賦值'public static Character character1 = 'A' 被編譯器優(yōu)化為
* '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未實現(xiàn)常量池機制驯杜,顯然數(shù)值類型的也不需要
* public static Double d1 = 1.0 優(yōu)化為 public static Double d1 = Double.valueOf(1.0D);
* 實際上也是調(diào)用 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未實現(xiàn)常量池機制,顯然數(shù)值類型的也不需要
* public static Float f1 = 1.0f; 優(yōu)化為 public static Float f1 = Float .valueOf(1.0f);
* 實際上也是調(diào)用 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的實現(xiàn)了常量池
* public static Integer i1 = 127;被編譯器優(yōu)化為
* 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的實現(xiàn)了常量池
* public static Long i1 = 127;被編譯器優(yōu)化為
* 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的實現(xiàn)了常量池
* public static Short i1 = 127;被編譯器優(yōu)化為
* 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
}
}