美文网首页
string 代码

string 代码

作者: lesline | 来源:发表于2019-03-15 09:31 被阅读0次

    String 代码

    
    package blog.string;
    
    import org.junit.Test;
    
    /**
     * intern 定义:
     * 如果常量池中存在当前字符串, 就会直接返回当前字符串. 如果常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回
     * <p>
     * java6 常量池在永久代
     * java7+java8  常量池在堆中 常量池中可以存储堆中的引用
     * Created by zhangshaolin on 2017/12/20.
     */
    public class StringIntern {
    
        @Test
        public void test() {
            String s = new String("1");
            String s2 = "1";
            System.out.println(s == s2); //false
    
            String s3 = new String("1") + new String("1");
            String s4 = "11";
            System.out.println(s3 == s4);//false
        }
    
        @Test
        public void test1() {
            String s = new String("1");
            String s2 = "1";
            System.out.println(s == s2);//false
    
            String s3 = new String("1") + new String("1");
            s3.intern();
            String s4 = "11";
            System.out.println(s3 == s4);//false
        }
    
        @Test
        public void test2() {
            String s = new String("1");
            String s2 = "1";
            s.intern();
            System.out.println(s == s2);//false
    
            String s3 = new String("1") + new String("1");
            String s4 = "11";
            s3.intern();
            System.out.println(s3 == s4);//false
        }
    
    
        @Test
        public void test3() {
            String s = new String("1");
            String s2 = "1";
            s.intern();
            System.out.println(s == s2);//false
    
            String s3 = "1" + "1";
            String s4 = "11";
            s3.intern();
            System.out.println(s3 == s4);//true
        }
    
        @Test
        public void test4() {
            String str1 = "a";
            String str2 = "b";
            String str3 = "ab";
            String str4 = str1 + str2;
            String str5 = new String("ab");
            System.out.println(str5.equals(str3));//true
            System.out.println(str5 == str3);//false
            System.out.println(str5.intern() == str3);//true
            System.out.println(str5.intern() == str4);//false
        }
    }
    
    
    
    package blog.string;
    
    import org.junit.Test;
    
    /**
     * 方法名、java等字符串在代码执行前已在常量池中存在
     * 所以s2.intern() 返回的是常量池中的数据
     * <p>
     * <p>
     * s1.intern() 返回的是常量池中的引用,引用的对象是堆中对象,所以=s1
     * <p>
     * <p>
     * Created by zhangshaolin on 2017/12/26.
     */
    public class StringInternJava {
        @Test
        public void test() {
            String s11 = "a" + "b"; //存在这个前后值不一样
            System.out.println(s11 == "ab");//true
        }
    
        @Test
        public void testA() {
            String s1 = new String("a") + new String("b");
            //String s11 = "a" + "b"; //存在这个前后值不一样
            /**
             是否存在 String s11 = "a" + "b";
             存在  不存在
             true    true
             false   true
             false   true
             */
            System.out.println(s1.intern() == "ab");
            System.out.println(s1.intern() == s1);
            System.out.println(s1 == "ab");
    
            String s2 = new String("ja") + new String("va");
            System.out.println(s2.intern() == "java");//true
            System.out.println(s2.intern() == s2);//false
            System.out.println(s2 == "java");//false
        }
    
        @Test
        public void testB() {
            String s2 = new String("java");
            String s3 = new StringBuilder().append("ja").append("va").toString();
    
            System.out.println(s2.intern() == s2);//false
            System.out.println(s2.intern() == s3);//false
        }
    
        @Test
        public void test1() {
            String s1 = new StringBuilder().append("aa").append("bb").toString();
            System.out.println(s1.intern() == s1); //true
            String s2 = new StringBuilder().append("ja").append("va").toString();
            System.out.println(s2.intern() == s2);//false
        }
    
        @Test
        public void test3() {
            String s1 = new String("aa") + new String("bb");
            System.out.println(s1.intern() == s1); //true
        }
    
        @Test
        public void test2() {
            String s1 = new String("aabb");
            System.out.println(s1.intern() == s1);//false
            String s2 = new StringBuilder().append("ja").append("va").toString();
            System.out.println(s2.intern() == s2);//false
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:string 代码

          本文链接:https://www.haomeiwen.com/subject/rjwkmqtx.html