| Author |
how many objects?
|
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
guys how many objects are there?
String s="ankur";
String s1=s.append("kothari");
K&B says there are the 3 objects ankur, kothari and ankurkothari but kothari would be lost as no one references it...........is this true? i think so but want to be sure
thanks
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
Yes it is true there is created three object of type string, and yes string "kothari" will be lost in memory.
String s="ankur"; //creating one object
String s1=s.append("kothari") //creating new string "kothari" and then append that string with s
|
SCJP 1.6 in progress ....
|
 |
Jeevan Reddy
Ranch Hand
Joined: Nov 10, 2009
Posts: 142
|
|
Dejan Miler wrote:Yes it is true there is created three object of type string, and yes string "kothari" will be lost in memory.
String s="ankur"; //creating one object
String s1=s.append("kothari") //creating new string "kothari" and then append that string with s
In that case, there should be only two objects.
How three?
|
SCJP 1.6 (94%)
http://faq.javaranch.com/java/JspFaq
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
String object is immutable and that means once when we create new String we cannot change it.
For example :
1.creating new String object.
2.creating new String object with the same value like s
3.here we had created two more objects "world" and "hello world"
4.and here we just reaching for s3 value (which is "Hello world")
|
 |
Jeevan Reddy
Ranch Hand
Joined: Nov 10, 2009
Posts: 142
|
|
Dejan Miler wrote:String object is immutable and that means once when we create new String we cannot change it.
For example :
1.creating new String object.
2.creating new String object with the same value like s
3.here we had created two more objects "world" and "hello world"
4.and here we just reaching for s3 value (which is "Hello world")
Now got it.
Thanks.
|
 |
Vidmantas Maskoliunas
Greenhorn
Joined: Nov 16, 2009
Posts: 22
|
|
|
Hi, here could be also a "string pool" effect in the game. I.e. string literals are not vanished after using them, and despite the fact that the string literal has no reference pointing to it, it still remains in the memory.
|
SCJP 6.0, willing to find Java job in NZ/AU and move there - LinkedIn profile - Java blog
|
 |
Dejan Miler
Ranch Hand
Joined: Nov 14, 2009
Posts: 56
|
|
|
Only if garbage collector do not free that memory.
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
Ankur kothari wrote:guys how many objects are there?
String s="ankur";
String s1=s.append("kothari");
K&B says there are the 3 objects ankur, kothari and ankurkothari but kothari would be lost as no one references it...........is this true? i think so but want to be sure
thanks
ankur , in fact for your example there will be no object on the heap. the reason is that your second line will not compile. there is no any append method in class string . check that.
avi sinha
|
SCJP 5.0 SCWCD 5.0
|
 |
Vidmantas Maskoliunas
Greenhorn
Joined: Nov 16, 2009
Posts: 22
|
|
Dejan Miller > In a typical case Garbage collector should not do that. See http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html about String Pools
avi sinha > Good shot, you have a sharp eye! I think to continue the discussion, we should replace append with concat
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
oh yes....it should be concat and not append.....
|
 |
 |
|
|
subject: how many objects?
|
|
|