aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringObjects 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 "StringObjects" Watch "StringObjects" New topic
Author

StringObjects

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

how many String Objects will be created in the following method...

public String makeString(){
String s="Fred";
s=s+"47";
s=s.substring(2,5);
s=s.toUpperCase();
return s.toString();
}

Answer given : 3

But I think total 5 objects will be created...: 1)Fred 2) 47 3)Fred47 4)ed4 5)ED4

Am I right ??? or If the answer given is right .. how it will be???
Kumaresh Vidhyasagar
Ranch Hand

Joined: Dec 05, 2008
Posts: 30
Your answer is correct!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Banu,

The String objects corresponding to the string literals are created before the method is executed, that's why you are only creating 3 String objects in the method.


All code in my posts, unless a source is explicitly mentioned, is my own.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Banu,

It's important to always include the source of any mock question that you discuss on this forum.

Where did this question come from?

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

Hi

This question is from -edit- an illegal source -/edit-

Ruben, I didn't get the point. Can you explain a bit clear...

The String objects corresponding to the string literals are created before the method is executed, that's why you are only creating 3 String objects in the method.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
When a class is loaded, java analyzes the source file and when it sees a string literal, it creates a String object for it, and adds a reference to it in the String Constant Pool (assuming a string literal with the same contents has not already been added.) That's why the String objects for string literals exist even before the main method starts executing. Take a look at this (The Java Language Specification):
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.5

Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 182

so in this method those 3 objects are : 1) Fred 2) 47 3)Fred47 . Correct?

public String makeString(){
String s="Fred";
s=s+"47";
s=s.substring(2,5);
s=s.toUpperCase();
return s.toString();
}

A new class instance may be implicitly created in the following situations:

* Execution of a string concatenation operator (ยง15.18.1) that is not part of a constant expression sometimes creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type.

Here concatenation opearator means only operator(+) or concat method also ???
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Banu Chowdary wrote:

public String makeString(){
String s="Fred"; // 1
s=s+"47"; // 2
s=s.substring(2,5); // 3
s=s.toUpperCase(); // 4
return s.toString(); // 5
}

The three objects created in the method are: "Fred47", "ed4", and "ED4".
Line by line:
1. No String object created at runtime (since "Fred" is a string literal.)
2. Here, we are creating "Fred47" (Notice that if the operation had been "Fred" + "47" no string would have been created at runtime, because you would be using the concatenation operator with two compile time constants (two string literals.))
3. A new String with the contents "ed4" is returned from the substring() method.
4. A new String with the contents "ED4" is returned from the toUpperCase() method.
5. No new String is created, since toString() returns the this reference.

As for the concatenation operator, that refers only to + and +=, yes.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

This exact same question has been discussed many times before on JavaRanch. Please do a search for "Fred 47" in the forums, and you'll find a whole list of old discussions:

http://www.coderanch.com/t/424671/Programmer-Certification-SCJP/What-output-related-to-string
http://www.coderanch.com/t/269588/Programmer-Certification-SCJP/Number-of-String-objects


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
 
subject: StringObjects