aspose file tools
The moose likes Java in General and the fly likes Doubt about creating a instance for String class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Doubt about creating a instance for String class" Watch "Doubt about creating a instance for String class" New topic
Author

Doubt about creating a instance for String class

gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
Hi All,

I have a doubt about the instance of String class.

eg:
I am declaring three string variable.
String name="myname";
String age = "myage"
String address = new String("myaddress");

please clarify me how many instance will String create.

3 or 1.

thanks in advance

GK
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You may want to take a look at Corey McGlone's JavaRanch article, Strings, Literally. I think this will make it much more clear.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 302
The String Literal Pool is refrenced in the article above.
When is the String Literal Pool created? At initialization of jvm or at runtime.
How long String Literal Pool will be maintained?
Does Strings refrenced by String Literal pool ever get garbage collected? Or exists until jvm exits.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9033
    
  10
Moving to Java in General (Intermediate)


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
I have another doubt on the same.
let us say
public class ImmutableStrings
{
public static void main(String[] args)
{
String test="Test";
test="Testing again";
System.out.println(test);
}
}

output is

Testing again

here the value of String Object test is changing.
Can anyone explain this?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4115
    
    2

You seem to be under the impression that the test variable actually "holds" the string value. It doesn't. It merely references the actual String instance. What is changed is the String object that test refers to, not the Strings themselves. The first assignment makes test refer to the String "Test", the second changes test to refer to the String "Testing again". The two String objects are still immutable.
[ December 01, 2005: Message edited by: Junilu Lacar ]

Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Thanks for the info lacar :-)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Doubt about creating a instance for String class
 
Similar Threads
doubt
Doubt
Doubt
doubt??
Doubt in this()