wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Query about String ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query about String ??" Watch "Query about String ??" New topic
Author

Query about String ??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,

Can any one please tell me what is the difference between :

String str = new String("abc");

and

String str1 = "abc";


Thanks & Regards
Bikash
Marcus Pant
Greenhorn

Joined: May 21, 2004
Posts: 7
imho, there is no difference...
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,

That means if i declare in any method

String str = new String("abc");

or

String str1 = "abc";

then at every call of this method it will create a new object of String class and allocate a new space for this object in the heap memory.Am I right ?

Thanks & Regards
Bikash
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
From the String API docco:

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

I think that means that if you instantiate a string identical to an existing one, the JVM may share the instance. However, I would not depend on that an I would always use .equals (someOtherString) method to compare two string references.
[ May 24, 2004: Message edited by: Eddie Vanda ]

The nice thing about Standards is that there are so many to choose from!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Actually, there's no question about it. The behavior is precisely defined.

Every time you use "new String()", a new String will be created.

String literals, however (i.e., "abc") are indeed shared. If the quoted String "abc" appears 10 times in the classes loaded by the JVM, there will be only one String object that represents these ten literals. So saying

String s = "abc";

will never create a new String object.

You should essentially never use

String s = new String("abc");

There are a few very specialized circumstances where it can make sense, but in the general case, and unless you know otherwise, you're just wasting resources. None of these special cases, by the way, involves passing a literal as the argument -- only another String variable.

If I'm reading some code and I see 'new String("abc")', that always makes me think that the programmer was fairly new to Java.


[Jess in Action][AskingGoodQuestions]
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Ernest,

First of all thanks for the reply.What I have understood that when we define

String str = "abc";

then it will share the one object(of value "abc") with all request which are accessing this method.

and when we define

String str = new String("abc");

then it will create a new object for each request which are accessing this method.

Am I right ? If I am wrong then please correct me.

Now another question is :

First I have defined

String str = "abc";

after that on some condition if i changed the value of str = "def"

then only it will create a new object(cos as per String API String values cannot be changed after they are created). Am I right ?

Thanks & Regards
Bikash
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305


Indeed, the idea of immutability prevents any String object from changing. All that changes are the reference variables. So, in the above case, "lostReference" will essentially be lost in memory (assuming no other references point to it), until the garbage man comes along and cleans it up.
[ May 24, 2004: Message edited by: Jeffrey Hunter ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Jeffrey Hunter:
[CODE]
So, in the above case, "lostReference" will essentially be lost in memory (assuming no other references point to it), until the garbage man comes along and cleans it up.


Well, no, actually. That's yet another complication: literals are held in a special string pool, and are thus always referenced. A String literal won't be garbage collected.
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
From what I understand, this depends on the specific JVM implementation. In large part, string literals in the constant pool are transparent, and not eligible for GC. However, certain JVMs (such as those which are space-optimized for mission-critical systems), may define references to string literals as opaque, and thus they become eligible for GC. The JVM specification is not concrete when defining a specific GC algorithm, so essentially the answer to this problem is one of those, it depends.
Tony Jackson
Ranch Hand

Joined: Feb 23, 2001
Posts: 45
Bikash,

To clarify a question: you said "...if i changed the value of str="def"..."

you cannot change a String. So if you say str="def", what is happening is that the reference str now points to a different string. If "def" was not already in the string pool, it will be created; but now str points to it. You cannot change a string.

Tony
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I found an interesting demo for Strings being GCed on JavaWorld:
does an object exist, if you can't test its identy?


http://home.arcor.de/hirnstrom/bewerbung
XinJi Chang
Greenhorn

Joined: Feb 18, 2004
Posts: 4
Hi,
I am a student who has studied JAVA just 3 months...
My teacher told me that if we use
then JAVA will find "abc" in the "String Pool",and refer A to "abc" in the pool.

if we use
JAVA will create a new String object on "heap".

But I am still confused...
Does it mean that if I use
and there will be TWO String object on the heap??
and Will String objects in the heap be Garbage collected if there is no reference refer to them?
[ May 25, 2004: Message edited by: XinJi Chang ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

XinJi Chang wrote:

here will be TWO String object in the heap??
and Will String objects in the heap be Garbage collected if there is no reference refer to them?


the answers are yes, and sort-of. you will have two separate objects on the heap, and two references (B and C). so if you use B==C, you will get false, but B.equals(C) will give true.

the second question gets a "sort-of", because you can never say something "will be" collected by the GC. they become ELLIGIBLE for GC, but that may or may not ever occur. There is no way of knowing what happens (short of special tools) or controlling it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
Consider Paul's rocket mass heater.
 
subject: Query about String ??
 
Similar Threads
Permutations - upper case and lower case
Struggeling with regexp
question regarding strings
String Immutable
Stack Overflow