• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about string

 
chang nanny
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Please look at the following code,

String x= new String("abc");

how objects are created?

Thanks.
 
Anuradha Shanmugam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chang,

Hope your question is not complete.

Anyway,

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

Regards,
Anu
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic