• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

how many objects?

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if garbage collector do not free that memory.
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh yes....it should be concat and not append.....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic