• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Strings

 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please have a look at the following question:

How many String objects are created when we run the following code.

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;

can anyone please give me the answer ?
 
Naina Si
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two objects
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three objects created here.

"Hello" and "Pal" is created at class load time as they are compile time constants, "HelloPal" at the time of the execution of the class.

Naseem
 
cathymala louis
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three objects created.
1. Hello
2. pal
3. Hello pal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic