aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Construction" Watch "String Construction" New topic
Author

String Construction

Wai Iu
Ranch Hand

Joined: Apr 04, 2000
Posts: 39
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!
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Wai Iu, Sree,

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

s1= s1.length()=0
s2= s2.length()=0
NullPointerException

<pre>

class Child1 extends Parent {
static void staticMethod() {}
//static final void staticFinalMethod() {}
void instanceMethod() {}
//final void instanceFinalMethod() {}
public static void main(String[] args) {
String s1=new String();
String s2=new String("");
String s3= null;
System.out.println("s1="+s1+" s1.length()="+s1.length());
System.out.println("s2="+s2+" s2.length()="+s2.length());
System.out.println("s3="+s3+" s3.length()="+s3.length());
}
}

</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).]
Wai Iu
Ranch Hand

Joined: Apr 04, 2000
Posts: 39
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.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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>

int[] intArray1 = null;
System.out.println(intArray.length) //NullPointerException

int[] intArray2 = new int[10];
System.out.println(intArray[-1])//ArrayInde...boundsException
System.out.println(intArray[10])//ArrayInde...boundsException
System.out.println(intArray[0])// This is Ok

</pre>
regds
maha anna
[This message has been edited by maha anna (edited April 18, 2000).]
Javix Protocol
Ranch Hand

Joined: Mar 21, 2000
Posts: 57
maha anna care to tell me about the intern() in String .

------------------
The Javix


Write Once , Run Anytime ,Anywhere Forever And Smile Happily.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Construction