常量设计与常量池

常量设计

常量:一种不会被修改的变量

Java中的常量使用 public static final 来定义

public final static double PI_NUMBER = 3.14;//引用常量时使用类名来引用,如:Constants.PI_NUMBER

一种特殊的常量,接口里定义的变量默认是常量

public interface Special {
	String color = "yellow";//default:public static final
	public void move();
}

常量池

作用:节省内存,共享访问
常量池:相同的值只存储一份,节省内存,共享访问
基本类型的包装类:
Boolean :true,false
Byte、Short、Integer、Long :-128~127
Character :0~127
Float、Double:没有常量池(缓存)

看下面的例子:

public class CacheTest {
	public static void main(String[] args) {
		Boolean b1 = true;  //true,false
		Boolean b2 = true;
		System.out.println("Boolean Test: " + String.valueOf(b1 == b2));//true
		
		Byte b3 = 127;     //-128~127
		Byte b4 = 127;
		System.out.println("Byte Test: " + String.valueOf(b3 == b4));//true
		
		Character c1 = 127;  //-128~127
		Character c2 = 127;
		System.out.println("Character Test: " + String.valueOf(c1 == c2));//true
		
		Short s1 = -128;  //-128~127
		Short s2 = -128;
		System.out.println("Short Test: " + String.valueOf(s1 == s2));//true
		
		Integer i1 = -128;  //-128~127
		Integer i2 = -128;
		System.out.println("Integer Test: " + String.valueOf(i1 == i2));//true
		
		Integer i3 = 128;  //-128~127
		Integer i4 = 128;
		System.out.println("Integer Test: " + String.valueOf(i1 == i2));//false,128不在[-128,127]的范围内
		
		Long l1 = -128L;  //-128~127
		Long l2 = -128L;
		System.out.println("Long Test: " + String.valueOf(l1 == l2));//true
		
		Float f1 = 0.5f;
		Float f2 = 0.5f;
		System.out.println("Float Test: " + String.valueOf(f1 == f2));//false,Float、Double没有常量池
		
		Double d1 = 0.5;
		Double d2 = 0.5;
		System.out.println("Double Test: " + String.valueOf(d1 == d2));//false
	}
}
字符串常量池:

Java为常量字符串都建立了常量池缓存机制

public class StringConstantTest {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		String s3 = "ab" + "c"; //都是常量,编译器将优化,下同
		String s4 = "a" + "b" + "c";
		System.out.println(s1 == s2); //true
		System.out.println(s1 == s3); //true
		System.out.println(s1 == s4); //true
		//s1,s2,s3,s4都指向的是常量池中"abc"的地址(可以极大的节省内存)
	}
}
包装类和字符串的两种创建方式
  • 常量式赋值创建,放在栈内存中(将被常量化)
Integer a = 10;
String b = "abc"
  • new对象进行创建,放在堆内存中(不会常量化)
Integer c = new Integer(10);
String d = new String ("abc");

这两种创建方式导致创建的对象存放的位置不同(即使他们的值相等)

两种创建方式之间的比较

分析BoxClassTest.java中的Integer类

  • 基本类型与包装类型比较,将对包装类自动拆箱
  • 对象比较,比较地址
  • 使用"+"会自动拆箱
public class BoxClassTest {
	public static void main(String[] args)
	{
		int i1 = 10;
		Integer i2 = 10;                // 自动装箱
		System.out.println(i1 == i2);   //true
		// 自动拆箱  基本类型和包装类进行比较,包装类自动拆箱
		
		Integer i3 = new Integer(10);
		System.out.println(i1 == i3);  //true
		// 自动拆箱  基本类型和包装类进行比较,包装类自动拆箱
		
		System.out.println(i2 == i3); //false
		// 两个对象比较,比较其地址。 
		// i2是常量,放在栈内存常量池中,i3是new出对象,放在堆内存中
		
		Integer i4 = new Integer(5);
		Integer i5 = new Integer(5);
		System.out.println(i1 == (i4+i5));   //true
		System.out.println(i2 == (i4+i5));   //true
		System.out.println(i3 == (i4+i5));   //true
		// i4+i5 操作将会使得i4,i5自动拆箱为基本类型并运算得到10. 
		// 基础类型10和对象比较, 将会使对象自动拆箱,做基本类型比较
		
		Integer i6 = i4 + i5;  // +操作使得i4,i5自动拆箱,得到10,因此i6 == i2.
		System.out.println(i1 == i6);  //true
		System.out.println(i2 == i6);  //true
		System.out.println(i3 == i6);  //false
	}	
}

分析StringNewTest.java中的String类

  • 常量赋值(栈内存)和new创建(堆内存)不是同一对象
  • 编译器只会优化确定的字符串,含有变量的字符串将不会自动优化
public class StringNewTest {
	public static void main(String[] args) {
		String s0 = "abcdef";
		String s1 = "abc";
		String s2 = "abc";
		String s3 = new String("abc");
		String s4 = new String("abc");
		System.out.println(s1 == s2); //true 常量池
		System.out.println(s1 == s3); //false 一个栈内存,一个堆内存
		System.out.println(s3 == s4); //false 两个都是堆内存
		System.out.println("=========================");
		
		String s5 = s1 + "def";    //涉及到变量,故编译器不优化
		String s6 = "abc" + "def"; //都是常量 编译器会自动优化成abcdef
		String s7 = "abc" + new String ("def");//涉及到new对象,编译器不优化
		System.out.println(s5 == s6); //false
		System.out.println(s5 == s7); //false
		System.out.println(s6 == s7); //false
		System.out.println(s0 == s6); //true 
		System.out.println("=========================");

		
		String s8 = s3 + "def";//涉及到new对象,编译器不优化
		String s9 = s4 + "def";//涉及到new对象,编译器不优化
		String s10 = s3 + new String("def");//涉及到new对象,编译器不优化
		System.out.println(s8 == s9); //false
		System.out.println(s8 == s10); //false
		System.out.println(s9 == s10); //false
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值