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

number of objects created?

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

String x=new String("xyz");
y="abc";
x=x+y;

how many string objects are created???


thanks & regards

srikanth reddy
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4 String objects are created.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are the four string objects?

one for x
one for y
one for storing an intermediate value?
one for storing the final value of x?

thanks
Sok
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String x=new String("xyz"); // two objects are created. xyz and new String
y="abc"; // one object is created. abc
x=x+y; // one object is created abcxyz

Total of four objects are created unless xyz and/or abc are already in the constant pool.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String x=new String("xyz"); // two objects are created. xyz and new String



This is real news to me. I thought only one object is created. But my question is, if ) creates two objects, (where one is a new String(), and the other is "xyz"), then how can the reference x (as in String x) point to two objects (new String() and "xyz" at the same time?

Thanks,

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

Got the answer to that one.

String x = new String ("xyz");

is equivalant to

char data[] = {'x', 'y', 'z'}; //--> Array Object #1
String x = new String (data); //--> String Object #2

That's two objects.

Thanks,

-Vijay
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was realy interesting one!.Thanks
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

Got the answer to that one.

String x = new String ("xyz");

is equivalant to

char data[] = {'x', 'y', 'z'}; //--> Array Object #1
String x = new String (data); //--> String Object #2

That's two objects.



That's not quite how it works. The original line of code contains a String literal. When the class is loaded, a String object is created on the heap based on the character sequence "xyz", and a reference to this String is retained in a pool. When the method containing this line is run, a new String is created on the heap, containing the same character sequence as the previously created String object. After this code executes, there are two Strings on the heap, each containing the character sequence "xyz".

If you compare the object references with x == "xyz", you'll find it returns false, meaning the references point to different objects.

In practical code, lines such as the one above (new String("whatever"))are impractical and wasteful, as they create unnecessary String objects. It's useful for testing purposes, however, to know what's going on, and that such a line results in two separate String objects.

Hope this helps!
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Steve,

Thank you very much. I guess I'm the third kind of the guy who cannot count!

-Vijay
 
I am Arthur, King of the Britons. And this is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic