my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

Anthony Yip
Ranch Hand

Joined: Aug 24, 2003
Posts: 30
How many objects created in the following:
String str1;
String strExample = new String("Hi");
Regards
Anthony Yip
SCJP
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
only 1.
str1 is just a string ref variable and not an object.
thanks,
deep
Anthony Yip
Ranch Hand

Joined: Aug 24, 2003
Posts: 30
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

Joined: May 02, 2001
Posts: 124
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

Joined: Aug 24, 2003
Posts: 30
Thanks for reply.
Regards
Anthony
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
I believe null is NOT an object. It is merely a type.
 
 
subject: String