Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? [Re:臧圩人]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-07-03 17:09 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
ok, I think this topic is really really *complicated* by us, and will really really confuse others.

To all the Java beginners, if you want to use Strings in Java, you can use any of the ways provided above, disregarding how many Object instances have been created whether in the stack or in the heap or the pool of string. (i.e. It is the JVM to handle all these magic things behind scene, we don't have to care about these at all, unless there is a specific reason you want to optimize and profile your application -- my 2 cent)

So as a general rule if you don't understand this issue, forget about this article and just go ahead and use the magic Java String.

-----------------------------------------------------------------------------------------------------------
To all the Java language syntax experts who want to know more about this issue like "臧圩人" and "andy_wang_5". After I read about the Javadoc of the String.intern(), I found something interesting:

intern() method is a native method and it will return a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings. In other words, it returns the object reference from the *pool of string* in the heap.
(A pool of strings, initially empty, is maintained privately by the String object.)

Hence, talking about how the Object instances are allocated in the memory, they are outlined as follows:

- new String()
creates an object instance in the heap and returns an object reference to the instance in the heap.
- "xyz" (create string)
creates an object instance in the "pool of string" (pool of string is also objects so created in the heap) and returns an object reference to the instance in the "pool of string". (Assuming no same strings created in the pool of string)
- intern() method
returns the object reference in the pool of string which has the same string content created in the heap. Otherwise this string object is added to the pool and a reference to this String object is returned. (NB. different object reference from the one in the memory heap I have all overlooked it)



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

话题树型展开
人气 标题 作者 字数 发贴时间
15279 JAVA面试题解惑系列(二)——到底创建了几个String对象? 臧圩人 5322 2008-06-30 18:19
9157 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? Joffeec 727 2008-07-01 07:31
9330 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? 臧圩人 37 2008-07-01 10:40
8989 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? JiafanZhou 423 2008-07-02 17:02
9245 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? 臧圩人 61 2008-07-02 22:33
9033 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? andy_wang_5 485 2008-07-03 08:58
9269 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? 臧圩人 43 2008-07-03 12:35
8762 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? JiafanZhou 2124 2008-07-03 17:09
9654 Re:JAVA面试题解惑系列(二)——到底创建了几个String对象? 臧圩人 122 2008-07-03 22:14

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923