Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about 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 "a question about string" Watch "a question about string" New topic

a question about string

chang nanny

Joined: Feb 15, 2004
Posts: 14

Please look at the following code,

String x= new String("abc");

how objects are created?

Anuradha Shanmugam

Joined: May 09, 2004
Posts: 8
Hi Chang,

Hope your question is not complete.


if your question is "how objects are created?" then the answer is when you use new keyword an instance to the String class is created in the heap.

if your question is "how many objects are created? " then the answer is 1


Anuradha<br />(SCJP 1.4)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Anu check this out:
new String("abc") == "abc" returns false. So a new object is created which is different to the object "abc" residing in the string pool. So if this is the first time "abc" is used, then you could say two objects are created.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: a question about string
It's not a secret anymore!