This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
String astr=new String(); String astr=new String(""); Are the above two line of codes the same when generating an empty string? Moreover, for the following two lines of codes: String bstr=null; String bstr=""; Are they the same? Please help, Thanks!
Wai, String astr=new String(); Here astr is not pointing to any string object. String bstr=new String(""); Here bstr is pointing to "" ("" is a string object too) string object. String astr=null;//astr doesn't hold any thing. String bstr="";//bstr is holding "" string object. Maha or anyone please correct me if i am wrong. I have a question myself. String astr=new String(""); Does it mean that now two "" string objects have been created one in the pool and one in memory?
String s1=new String(); String s2=new String(""); Both are SAME Both statements create an emptyString in the heap. In fact in this code from this post there are 2 empty strings created in the heap memory. Both have nothing in it and length = 0 (which is no. of chars in the String object).
String s1=null; String s2=""; Both are NOT SAME Because s1 object is NOT created yet. It points to nothing(null). s2 is created object and it is an empty string. empty string is NOT SAME as null string in Java Also note that don't bother about the literal pool for cert purpose. In the above statements only 2 String objects are created. The foll code prints at run time
</pre> Sree, String astr=new String(""); For this you can be sure of ONE String object created in the heap. You also can be sure there is another one exists in the String literal pool of the program. But you can't be sure that the literal String "" is freshly created due to this particular statement alone or not. Do you get that point? If there would have been another statement in the program which used "" then , for your statement now this "" object is NOT BRAND NEW. It is the same old wine in a new bottle . The already created String literal "" address is returned now. This is the reason I said you can't be sure it is freshly created object now. Also note that you can intern your created object which is now in the heap, to the literal pool, which means you are making another copy of your object and give it to the literal pool. Now what happens? If there happened to be already existing copy in literal pool , then this old literal's address is returned. If not this String is really interned made another copy in literal pool and this new literal pool address is returned from this String.intern() statement. From next time onwards if you create the String literal which is IDENTICAL to the interned String, now Java returns the same old interned copy, not any more new copy in the literal pool. For the exam purpose don't bother about the literal pool's copy. regds maha anna
[This message has been edited by maha anna (edited April 17, 2000).]
Joined: Apr 04, 2000
Thanks, Anna and Sree. Your comments really help me understand the problem. Another question, when does a program generate a NullPointerException and when does it generate an ArrayIndexOutOfBoundsException? Sometimes, I am just confusing about these two kinds of RuntimeExceptions.
Joined: Jan 31, 2000
Wai Iu, It is like this. When a reference is null and you try to invoke some member of its class type, for example nullref.someMethod() or nullref.somevar then a NullPointerException will be thrown at run time. Note that this rule is applicable for all ref type (class/interface/arrays). The ArrayIndexOutOfBoundsException is different. THis is thrown at run time when you have non-null reference, in other words the reference is already assigned to a solid physical array, and you try to access an element of the array by giving index value which falls out of its current size. Which means if an array has length = 10 (0,1,2,3,4,5,6,7,8,9) you can give index values anything bet 0(inclusive),1,2,3...9(inclusive). <pre>