GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes string object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "string object" Watch "string object" New topic
Author

string object

kali kinkar
Greenhorn

Joined: Nov 23, 2000
Posts: 1
pls any body help me out
what is the defferece in these two?
String s1;
String s2=null;
is it both are same?

String s1="Hello";
String s2=new String("Hello");

thanks
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Regarding your first question, Thay are both the same. The default for strings that are not inititalized is null.
For your second question, String s1="Hello"; is created in the string pool and is not eligible for garbage collection. while
String s2=new String("Hello") is created on the heap and is eligible for GC.
I hope this helps.
Bosun

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Oliver Grass
Ranch Hand

Joined: Nov 02, 2000
Posts: 65
Hey Bosun,
are you sure???
Originally posted by Bosun Bello:
[For your second question, String s1="Hello"; is created in the string pool and is not eligible for garbage collection. while
String s2=new String("Hello") is created on the heap and is eligible for GC.

What has a string literal in the string pool to do with being eligible for garbage collection??? IMHO you can still say s1 = null and then s1 is eligible for garbage collection. The literal will stay in the string pool, but that's only how JVM optimizes string literals.
I'm not sure if your statement is completly right...
Perhaps someone can help or you could explain me, why do you think that s1 is not eligible for garbage collection....
cheers
Oliver

[This message has been edited by Oliver Grass (edited November 23, 2000).]
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
I disagree that
String s1;
and
String s2 = null;
are identical.
The first one just declares a variable but doesn't initialise it. The second one declares and initialises a variable, even though it's a null variable. If s1 is a class member variable, it won't matter because the compiler will effectively replace
String s1;
with
String s1 = null;
But if it's a local variable it will matter - the compiler won't let the program compile - it will give you a warning that s1 may not be initialised.
To see what I mean, try running this code:-
class StringTest
{
String s1;
String s2 = null;
public static void main (String args[])
{
// String s3;
// String s4 = null;
StringTest example = new StringTest();
System.out.println ("String one is " + example.s1);
System.out.println ("String two is " + example.s2);
// System.out.println ("String three is " + s3);
// System.out.println ("String four is " + s4);
}
}
and then remove the comment slashes so that the commented lines are included in the programme and see what happens.
I'm not sure on your query, Oliver - the literal pool is a little bit murky from where I'm standing! I'd be interested to see anyone else's view on that one.
Kathy
 
GeeCON Prague 2014
 
subject: string object