• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

how many objects

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1="abc";
String s2="xyz";
String s3=new String("abcd");
how many object will get created?
Some peole say 4 objects..is it right
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I remember, abc and xyz are created in the String pool and abcd is created in the pool and the JVM memory. So the answer should be 4

I ll let other ranchers confirm that though.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the Deepak Bala is right.

For more information, see the Page 415(Ch VI) of the book titled "SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)" written by Kathy Sierra and Bert Bates.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi varinder

its right. 4 objects will be there. if we give any String in double quotes, one objects will be created in pool. if the same string is present, it will be reused, otherwise a new one will be created. here, you ha created three string objects using double quotes. And you had used the constructor of String also(new String("....") . this will also create an instace of String class. So the total number of objects will be four.

Thanks

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


Hi varinder
its right. 4 objects will be there. if we give any String in double quotes, one objects will be created in pool. if the same string is present, it will be reused, otherwise a new one will be created.



Hi Anoob,

Just reconfirm your statement above, so regarding below code it only creates 3 String, right??

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


yes above code creates only three objects
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
------------------------------------------------
String s1="abc";
String s2="xyz";
String s3=new String("abcd"); //line1
how many object will get created?
Some peole say 4 objects..is it right
------------------------------------------------
As Anoobkumar said...Here 4 objects are created...
3 objects are created using double quotes...
and one is created using constructor...

If line1 is changed as
String s3=new String("abc");

Then in this case 3 objects are created....
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vierda Mila

yes it will create only three objeects.
 
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Some minutes ago I thought that there are three objects, but actually 4 objects are created. Three object with double quotes and one using constructor. Lets make some test:

system.out shows that temp and s3 do not refer to the same object, so there were 4 objects created.
[ August 28, 2008: Message edited by: Alexey Saenko ]
 
Siva Sekhar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we use new keyword like this
String ref = new String("some");

Here new key word creates one object in the memory and the ""(quotes)
creates another object in the pool...

So here 2 objects and one reference is created...
 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am really confused,still we are in a fix....
some people say 3 and some say 4...
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think some people say 3 because they change the code
be like this:

String s1="abc";
String s2="xyz";
String s3=new String("abc"); --> different with yours (String s3=new String("abcd")
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to know how much objects are created in single program ??
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s1="abc";
String s2="xyz";
String s3=new String("abcd");
four object will be created,
"abc" and "xyz" in string poor,
if we use keywords new to creat string class,one object will in the string poor,and the other created in memery, so,"abcd" will created twice;
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic