File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Strings 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 "Regarding Strings" Watch "Regarding Strings" New topic

Regarding Strings

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
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

Joined: Nov 05, 2003
Posts: 134
two objects
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
Three objects created.
1. Hello
2. pal
3. Hello pal
I agree. Here's the link:
subject: Regarding Strings
It's not a secret anymore!