常量设计

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

  • Java是没有const关键字
  • 不能修改,final
  • 不会修改/只读/只要一份,static
  • 方便访问,public

Java中的常量

  • public static final
  • 建议变量名字全大写,以连字符相连,例如:UPPER_BOUND
public class Constants{
    public final static double PI_NUMBER=3.14;
    public static final String DEFAULT_COUNTRY="China";
    public static void main(String[] a)
     { 
           System.out.println(Constants.PI_NUMBER);
           System.out.println(Constants.DEFAULT_COUNTRY);
     }
}

一种特殊的常量:接口内定义的变量默认为常量

public interface SpecialAnimal{
	String color = "yellow";//default:public static final
	public void move();
}
public class Cat implements SpecialAnimal{
	public void move() {
		System.out.println("I can move");
	}
	public static void main(String[] args)
	{
		Cat cat=new Cat();
		cat.color="white";//error,the variable in interface are constants. 
	}
}

常量池

-Java为很多基本类型的包装类/字符串都建立常量池。
-常量池:相同的池只存储一份,节省内存,共享访问。
-基本类型包装类

  • Boolean:true,false
  • Byte,Character:\u000--\u007f(0--127)
  • Sort,Int,Long:-128~127
  • Float,Double:没有缓存(常量池)
  • Java为常量字符串都建立常量池缓存机制

eg:

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;
	}
}

-基本类型的包装类和字符串有两种创建方式

  • 常量式(字面量)赋值创建,放在栈内存(将被常量化)
Integer a=10;
String b="abc";
  • new对象进行创建,放在堆内存(不会被常量化)
Integer c=new Integer(10);
String d=new String("abc");

这两种创建方式导致创建的对象的存放位置不同。

装箱与拆箱问题
1.

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
	}	
}

-基本类型和包装类比较,并对包装类自动拆箱。
-对象比较式在比较地址。
-加***自动拆箱

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
	}
}

-常量赋值(栈内存)和new创建(堆内存)不是同一个对象
-编译器只会优化确定的字符串,并缓存