File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Anthony Yip
Ranch Hand
Posts: 30
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.
Regards
Anthony
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe null is NOT an object. It is merely a type.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic