Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many objects?

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeevan Reddy
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dejan Miler
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if garbage collector do not free that memory.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vidmantas Maskoliunas
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes....it should be concat and not append.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic