• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about String ??

 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
imho, there is no difference...
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found an interesting demo for Strings being GCed on JavaWorld:
does an object exist, if you can't test its identy?
 
XinJi Chang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic