• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Objects

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I have small doubt in the String manipulation,
String s="test",s1="test",s2="test",s3="test";

For this , How many string objects to be created ?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think is the answer?
Why?
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't get confused by a number of String declarations and initilizations in a single line. Just count the string references and go ahead.

tell us your answer and we'll verify.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi anubhav i agree we can just count the number of references, however it is interesting to bring up the working of string pool here.
In the above code if we add the following:-

System.out.println(s1==s2);

We get "true", the point being that s1 and s2 refer to the same object.
So.....now how many objects do we have???
[ April 08, 2008: Message edited by: Rajat Asani ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have small doubt in the String manipulation,
String s="test",s1="test",s2="test",s3="test";


I think this is the explanation.

you can rearrange above code like this also.

String s= "test";
String s1= "test";
String s2= "test";
String s3= "test";


if its like this

String s= "test";
String s1= new String("test");
String s2= new String("test");
String s3= new String("test");

each time a new String object will be created and assigned to the reference
variables s, s1, s2, s3 respectively.
so there are totally 4 String objects created in heap.


but since you declare it with string literals, the first time a string literal is found its added to string constant pool memory.

Next time onwards if you declare another String literal with same String content value its not created afresh and previously created string literal will be reused and its address will be assigned to second reference and so on..


i.e,

[1]String s= "test"; //"test" is placed in String constant pool


[2]String s1= "test";// here no String object is created instead s1 contains the address of above String object created at line [1]

//likewise all s2 and s3 will contain address of Object created at [1]
String s2= "test";
String s3= "test";

so ultimately only 1 object is created.

I hope I answered correctly.
[ April 08, 2008: Message edited by: vijaya saradhi ]
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Me too, I think there is just one object created, because the others references (S1,S2 etc...) are just refering the same object that is already created in the pool.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll stick my neck out here and say none!
Only because there is no "new" object created only references to the the same string literal.
Anyone else agree/disagree?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i would say both of them are correct tentatively based on the context!

If at all the question has been asked that "how many objects are created in this method?" and all the aforementioned strings are part of a method, then th e answer is none.

Why because, the string "test" is being a literal,it is created when the class is being loaded. If it is having a "new", of course definitely there is a brand new string object created for you in heap!

Had they been an instance variables, there is only one string object created during the class loading operation!

Hope this helps! :cheers:
[ April 08, 2008: Message edited by: Raghavan Muthu ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover you don't need to bother much about it if at all this question was based on the Garbage Collection, as Bert bates had been telling that the exam has taken out the Garbage Collection questions which involves Strings as the strings are never ever GC'ed because of String Literal Pool.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajat Asani : Hi anubhav i agree we can just count the number of references, however it is interesting to bring up the working of string pool here.



Indeed Rajat and that was the job here.

Well, I agree with the explanation by Raghavan. Depending on the context there will be zero(in method) or one (instance variable) object created.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic