Important Facts About Strings and Memory

       前几天看《Java2学习指南》这本书时,解决了一个以前困惑的问题,感觉茅塞顿开,原来和我想的一样。问题如下。
class NewString{
  public static void main(String [] args){
    String s1="java";
    String s2="java";
    String s3=new String("java");
    if(s1==s2)
        System.out.println("s1==s2");
    else
        System.out.println("s1!=s2");
    if (s1==s3)
    {
       System.out.println("s1==s3");
    }
    else
        System.out.println("s1!=s3");
    }
}

执行结果:

s1==s2
s1!=s3
我一直认为Java在处理字符串时,在内存中开辟了一块专门的区域,如果执行如String s2="java";这样的语句时先到此区域中看是否已存在值相同的String对象,如果存在,引用型变量s2就指向它。《Java2学习指南》这本书验证了我的想法。摘录书中内容如下:
Important Facts About Strings and Memory
In this section we’ll discuss how Java handles string objects in memory, and some of
the reasons behind these behaviors.
One of the key goals of any good programming language is to make efficient use
of memory. As applications grow, it’s very common that String literals occupy large
amounts of a program’s memory, and that there is often a lot of redundancy within
the universe of String literals for a program. To make Java more memory efficient,
the JVM sets aside a special area of memory called the “String constant pool.” When
the compiler encounters a String literal, it checks the pool to see if an identical String
already exists. If a match is found, the reference to the new literal is directed to the
existing String, and no new String literal object is created. (The existing String
simply has an additional reference.) Now we can start to see why making String
objects immutable is such a good idea. If several reference variables refer to the same
String without even knowing it, it would be very bad if any of them could change
the String’s value.
You might say, “Well that’s all well and good, but what if someone overrides the
String class functionality; couldn’t that cause problems in the pool?” That’s one of
the main reasons that the String class is marked final. Nobody can override the
behaviors of any of the String methods, so you can rest assured that the String objects
you are counting on to be immutable will, in fact, be immutable.
Creating New Strings
Earlier we promised to talk more about the subtle differences between the various
methods of creating a String. Let’s look at a couple of examples of how a String might
be created, and let’s further assume that no other String objects exist in the pool:
1 - String s = "abc"; // creates one String object and one reference
// variable
In this simple case, “abc” will go in the pool and s will refer to it.
2 - String s = new String("abc"); // creates two objects, and one
// reference variable
In this case, because we used the new keyword, Java will create a new String
object in normal (nonpool) memory, and s will refer to it. In addition, the literal
“abc” will be placed in the pool.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值