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 How Many String Object? 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 "How Many String Object?" Watch "How Many String Object?" New topic

How Many String Object?

Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi Just want to know one thing:

How many String Object are created when this method is invoked?

public void stringObject()
String str = "Java";
str = str + "Ranch";
If we see the question :: It is asking "When the method is invoked ?"

So it means that at the run time ..!!
So i think answer should be
"Java" and "Ranch" object will be created at compile time..!!
only Java Ranch will be created on the run time..!!

Please correct me..!!

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
I don't think they are very particular about run time creation or compile time . In general three string objects are created in the pool. Correct me if am wrong

m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
2 objects is created in pool

1 is 'java' and assigned to 'str' refernce
2 is 'java ranch' assigned to 'str' refernce

==>the 'java' will be now abandoned in the string pool without any refernce.

i 2 not sure abot compile time and run time object creation.

Deepak Chopra
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

but i think that..they consider run time and compile time..because this is the only thing that can make a string question little tricky..!!!
Serg Masow
Ranch Hand

Joined: Dec 17, 2007
Posts: 50

I agree with Sid there are three string objects "Java", "Ranch" and "JavaRanch". I think the question isn't about the creation of string at runtime.


SCJP 6.0 [95%] OCE EJBD 6.0 [93%]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Obviously, there are three String objects getting created. But the question asks about when it gets created depends on the type of String being created.

First two string constants (literals) "Java" and "Ranch" are getting created during the class loading operation and the concatenated string (the + operator returns a new String object) causes a brand new string to be generated at the runtime.

You may have a look at these threads for further exposure in this regard.

  • String Concatenation
  • How many are objects are eligible for GC?

  • Hope this helps!
    [ January 31, 2008: Message edited by: Raghavan Muthu ]

    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    I agree. Here's the link:
    subject: How Many String Object?
    It's not a secret anymore!