常量设计和常量池

一.常量设计

1.常量 pubiic static final。
2.常量名字建议全部大写,比如UPPER_BOUND。
3.特殊的常量:接口内定义的变量默认是常量

比如:

public interface inte {
    String color ="yellow";//default :public static final String color
    
    private String def="123";//private is not allowed here
}

对象的双等号比较指针是否指向同一个东西

二.常量池

观察一下代码输出结果

public class Test{

    public static void main(String[] args) {
  Integer n1=127;
  Integer n2=127;
  System.out.println(n1==n2);    //true
  Integer n3=128;
  Integer n4=128;
  System.out.println(n3==n4);    //false**
  //因为 Integer 128不在常量池
  Integer n5=new Integer(127);
  System.out.println(n1==n5);    //false

    }
}

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

常量池包含的内容

1.基本类型的包装类(注意Float和Double没有常量池)
Boolean: true false
Byte:0-127
Short :-128-127
Integer:-128-127
Long :-128-127
Character:0-127
2.String

public class Test{

    public static void main(String[] args) {
String s1="abc";
String s2="a"+"b"+"c";//都是常量,编译器将会优化
System.out.println(s1==s2);

    }
}

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

1.赋值,放在栈内存(速度快但是内存小)

Integer a=10;
Stirng b=“abc”;

2.new对象创建,放在堆内存(速度慢但是内存大)

Integer c=new Interger(10);
String d=new String(“abc”);

例题(重点)


public class BoxClassTest {
    public 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);
//(i4 + i5)会让对象自动拆箱,做基本类型比较=10
//基础类型10和对象比较会让对象自动拆箱,做基本类型的比较
        //true
        System.out.println(i2 == i4 + i5);
        //true
        System.out.println(i3 == i4 + i5);
        //true
        Integer i6 = i4 + i5;
        System.out.println(i1 == i6);
        System.out.println(i2 == i6);
        System.out.println(i3 == i6);
    }
}

例题(重点)


public class StringNewTest {
    public 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 = "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";//不优化
        String s9 = s4 + "def";//不优化
        String s10 = s3 + new String("def");//不优化
        System.out.println(s8 == s9);//false
        System.out.println(s8 == s10);//false
        System.out.println(s9 == s10);//false
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值