Java知识梳理04|String内存分析(JDK1.8)

"本文详细探讨了Java中的String类,包括它的不可变性、内存分配以及在不同创建方式下的表现。通过实例展示了`String str = "Hello"`与`String str = new String("Hello")`的区别,以及字符串拼接时在常量池和堆中的行为。此外,还深入讲解了`intern()`方法的工作原理,如何影响字符串池,并通过示例说明了其在内存管理和效率优化中的作用。"

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String的基础知识

  • String是final类,不能被继承,并且其方法都被final修饰。
  • String的任何操作都不会影响到原来的String对象, 所有的改变都会创建新String对象。

String的内存分析

String str = “Hello”

public class stringclass {
    public static void main(String[] args) {
       	String str="Hello";
        String str2="Hello";
        System.out.println(str==str2);
        str="World";
    }
}
//输出结果:
true

String str = new String (“Hello”)

public class stringclass {
    public static void main(String[] args) {
  			String str= new String("Hello");
        String str2= new String("Hello");
        String str3 = "Hello";
        System.out.println(str==str2);
        System.out.println(str==str3);
    }
}  
//输出结果:
false
false

String str = “Hello” + “World”

public class stringclass {
    public static void main(String[] args) {
        //当一个字符串由多个字符串常量连接而成时,它自己肯定也是字符串常量。
        //该字符串是在编译期就能确定。先是在池里生成“a”和“b”,再通过拼接的方式在池里生成"ab"。
        String str="Hello" + "World";
    }
}

String str = new String (“Hello”) + new String(“World”)

  • 当使用了变量字符串的拼接(+, sb.append)都只会在堆区创建该字符串对象, 并不会在常量池创建新生成的字符串。
public class stringclass {
    public static void main(String[] args) {
        String str=new String("Hello") + new String("World");
    }
}

String 的 intern() 方法

    /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>
     * It follows that for any two strings {@code s} and {@code t},
     * {@code s.intern() == t.intern()} is {@code true}
     * if and only if {@code s.equals(t)} is {@code true}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @return  a string that has the same contents as this string, but is
     *          guaranteed to be from a pool of unique strings.
     */
    public native String intern();
  • 当调用intern方法时,如果池已经包含与equals(Object)方法确定的相当于此String对象的字符串,则返回来自池的字符串。 否则,此String对象将添加到池中,并返回对此String对象的引用。
//例子1
public class stringclass {
    public static void main(String[] args) {
        String str="Hello";
        String str2=new String("Hello");
        String str3=str.intern();
        String str4=str2.intern();
        System.out.println("str==str3:"+(str==str3));
        System.out.println("str==str4:"+(str==str4));
        System.out.println("str2==str3:"+(str2==str3));
        System.out.println("str2==str4:"+(str2==str4));
        System.out.println("str3==str4:"+(str3==str4));
    }
}
//输出结果:
str==str3:true
str==str4:true
str2==str3:false
str2==str4:false
str3==str4:true

//例子2
public class stringclass {
    public static void main(String[] args) {
        String str=new String("Hello")+new String("World");
        String str2=str.intern();
        String str3="HelloWorld";
        System.out.println("str==str3:"+(str==str2));
        System.out.println("str==str4:"+(str==str3));
        System.out.println("str2==str3:"+(str2==str3));
    }
}
//输出结果:
str==str2:true
str==str3:true
str2==str3:true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值