java8 str.intern() 测试

Wesley13
• 阅读 517
package io.openmessaging.storage.dledger;

public class T {

    public static void main(String[] args) {
        /* 
         * String str = new String("a"); //产生常量a ,堆上的new String
         * str = str.intern();   //找到常量池的a
         * 
         * String str2 = "a"; //常量池中的a
         * 
         * System.out.println(str == str2);
         */

        /*String s5 = new String("hi") + new String("j");

        s5.intern();

        String s6 = "hij";

        // s5 == s6 ? true
        System.out.println(String.format("s5 == s6 ? %s", s5 == s6));*/
        
        
        String s5 = new String("hi") + new String("j"); //产生两常量hi,j  堆上的new String

        s5.intern(); //将s5的引用加入常量池

        String s6 = "hij";//s6指向s5
        s6 = s6.intern();

        // s5 == s6 ? true
        System.out.println(String.format("s5 == s6 ? %s", s5 == s6));
        
        String s7 = new String("hij");
        s7 = s7.intern();
        System.out.println(String.format("s5 == s6 ? %s", s5 == s7));
        
        
        
        
        String s = "zzn" + 2L;
        String s2 = "zzn" + 2L;

        System.out.println(s == s2);
        
        

        String s3 = "zzn" + 222222L;
        String s4 = "zzn" + 222222L;

        System.out.println(s3 == s4);
        
        System.out.println(add(44444L) == add(44444L));
        System.out.println(add2(44444L) == add2(44444L));
    }
    
  
    public static String add(Long l) {
        return "zz"+l;
    }
    
    public static String add2(Long l) {
        return ("zz"+l).intern();
    }

}


s5 == s6 ? true
s5 == s6 ? true
true
true
false
true

总结:

jdk1.6:

str.intern()返回常量池对象

jdkl1.8:

str.intern();

当str是常量String  str="a”,此时常量池中不存在常量或者引用,那么将常量“a”加入常量池。如果常量池中存常量或者引用,那么str指向常量池中的常量或者引用。

当str是指向堆中的引用String  str=new String("hi") + new String("j"),此时常量池中不存在常量或者引用,那么将常量“hij”得引用str加入常量池。 如果常量池中存常量或者引用,那么str指向常量池中的常量或者引用。

常量池只会保留一份,要么是常量,要不是指向堆中的引用

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java8 日期类库基本使用
java8日期类库基本使用publicstaticvoidmain(Stringargs){/java比较两个日期的差年月日等/DateTimeFormatterdateTimeFormatter
Wesley13 Wesley13
2年前
java中使用redis
1.java代码publicclassRedisTest01{publicstaticvoidmain(Stringargs){//connectredisserverJedisredisnewJedis("127.0.0
Wesley13 Wesley13
2年前
java第五次上机
1.实现如下类之间的继承关系,并编写Music类来测试这些类。packageMusic;publicclassMusic{publicvoidtune(Instrumenti){i.play();}publicstaticvoidmain(Stringargs\\){Musicanew
Wesley13 Wesley13
2年前
java读取按行txt文件
importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.InputStreamReader;publicclassT{publicstaticvoidmain(Stringargs
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Wesley13 Wesley13
2年前
Java根据当前日期获得这一周的日期
Java根据当前日期获得这一周的日期,不是很经常用,但是用起来又不是很好找的代码!/测试@paramargs/publicstaticvoidmain(Stringargs){//定义输出日期格式SimpleDateFormatsd
Wesley13 Wesley13
2年前
JAVA中的IO
FILE类常量importjava.io.;classhello{publicstaticvoidmain(Stringargs){System.out.println(File.separator);//输出"/"Syste
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
京东云开发者 京东云开发者
5个月前
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了