Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of String objects

 
Karthick Ravi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, got this question which I fail to understand. Hellppppppp....


Q How many String objects are created in the method?

public String returnString() {
String str = “Hello”;
str = str + “World”;
str = str.toLowerCase();
str = str.substring(2, 5);
return str.toString();
}


My understanding:
String objects are immutable. Hence, any manipulation of an existing object is done by creating a new String object.

Therefore. My answer is that the number of objects created are 6. The list is

1. "Hello"
2. "World"
3. str + “World”
4. str.toLowerCase();
5. str.substring(2, 5);
6. str.toString();


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthick Ravi wrote:Guys, got this question in a bank ...

In a bank, what exactly do you mean by that? Please quote your sources.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure. I see the String Source Code.

i dont think str.toLowerCase() and str.toString create Objects. so the object creations are 4
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to me 3 objects are created in pool and 3 on the heap.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chander shivdasani wrote:According to me 3 objects are created in pool


String Constant Pool is nothing but Collection Of references which is pointed to an Object in Heap
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My understanding is that the second operand "World" (from x + y) is not created, because we have here a call to the concat method str.concat("World"), where "World" is just a parameter, which is not a string object (EDIT: here is an error. look at my or Tomasz post !!!) (lookup the method concat in string class).
The toLowerCase()-method creates a new object with the new keyword, like substring(), whereas toString()-method just returns the string itself(return this;)

String Constant Pool is nothing but Collection Of references which is pointed to an Object in Heap

Are you sure about this? I thought there are string objects in the pool as well.

cheers
Bob
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chander shivdasani wrote:According to me 3 objects are created in pool and 3 on the heap.


I think it is 5 in total....But the answer is 3
 
Tom Kowalski
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why the answer is 3? Where do you found this answer and why do you think it is true?

In K&B there was quite simillar example


It is also written that String which were made are: 'spring', 'summer', 'spring summer'. So the beginng is quite similar to the example which you have given. So I think we should agree that in this two lines, there are three objects.

In my opinion:

 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: Tomasz changed his post during my upload/writing.

Tomasz Stachlewski wrote:
In my opinion:

Ok. The string parameter of str.concat("World") or str + "World" creates a String object "World". I was wrong there. So we have 5 objects.
But str.toString() doesn't create a new object. The object referenced by str already exist.

cheers
Bob
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** REMOVED **
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc, I thought the same two post above, but I had to rectify quickly and erase all my post. The first line:

creates a new String object as says the Java API specification:
All string literals in Java programs, such as "abc", are implemented as instances of this class.

Furthermore the string literals are linked to the string literal pool (which contains only references to objects, not objects). The String creation on the heap is mandatory. I think the given answer is wrong.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** removed **
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Vo wrote:
David, the first three lines will be optimised into
String str = "HelloWorld";
Hence JVM won't create String literals "Hello" nor "World".


Disagree .
 
Karthick Ravi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In K&B there was quite simillar example



This was actually brought up in a study group apparently which is a modification of the above question. They also don't have a definite answer. The answer was given as 3 earlier as my birthday present to leave me confused for the rest of the day.
But it has brought some rare insights.
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think we can assume any compiler optimizations for the sake of exam. Otherwise, results can be different depending on which compiler user chooses. So work through the examples assuming details given only in the Java Language Specification.

Having said that and assuming no compiler optimizations, answer for this question is 5 and as follows. The list of objects/values shows the snapshot of heap after a corresponding statement is executed. (lost) indicates that object doesn't have a valid reference.


 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:
Disagree .


I tend to agree with you now after doing some research. So I'll remove my previous posts:

Anyway, here is what the bytecode looks like after compiling the above code:



So there are "Hello", "World", "HelloWorld", "helloworld", and "llo".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic