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

Creating String Objects

 
Sumi Festus
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
this was a question i stumbled upon
String x=new String("xyz");
String y="abc";
String x=x+y;

The answer says that 4 String objects are created can someone give me an explanation as to how 2 string objects are being created in the first line...
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String instance is created that represents the String literal, which is passed to the String(String) constructor to create yet another String instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic