This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Anthony Yip
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many objects created in the following:
String str1;
String strExample = new String("Hi");
Regards
Anthony Yip
SCJP
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only 1.
str1 is just a string ref variable and not an object.
thanks,
deep
 
Anthony Yip
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm..........
Shouldn't it be 3 objects?
str1 will be given a default value as null, so there is a null object created.
strExample has a String object "Hi" created and passed to the new constructor to created another String object that assigned to strExample later. So two object has been created.
I just can't sure whether the null one should be counted or not.
Regards
Anthony Yip
 
Harry Singh
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There are two String objects created at line 2.The null string object will not be a new object so there are two string objects created on a heap.
Regards,
Harjinder
 
Anthony Yip
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
Regards
Anthony
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe null is NOT an object. It is merely a type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic