DESKTOP-P665UA2_20200828-石家雨

问题

char[] data = { 'h' , 'e' , 'l' , 'l' , 'o' };
        
        String x = new String( data );
        String t = x.intern();
        String s = "hello" ;
 
        System.out.println( s == x );  // true
        System.out.println( t == s );  // true
        System.out.println( x == t );  // true

char[] 数存放在堆里,“hello”存放在字符串池里

调用x.intern()时, 会首先检查 字符串池 中是否存中与x相等的String实例,

char[] data = { 'h' , 'e' , 'l' , 'l' , 'o' };
        
        String x = new String( data );
        String s = "hello" ;
        String t = x.intern();
 
        System.out.println( s == x );  // false
        System.out.println( t == s );  // true
        System.out.println( x == t );  // false

当出现"hello"时会首先查找 字符串池 中是否已经存在 与之相等的 String实例,

若存在则返回字符串池中的String实例的引用

吐槽

标签


© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1