• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

StringObjects

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your answer is correct!
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

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

 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic