File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringObjects

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer is correct!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic