• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.toString() and Integer.toString()

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi here i am running this program and i got false for the first code and for the 2nd code i got true

here both b1 and b2 are reference types which hold object
but why jvm gives different o/p's???
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From java docs i found out this..
When toString is invoked on each of the class..

String:-
"This object (which is already a string!) is itself returned."
So each time same object returned.

Integer:-
"Returns a String object representing this Integer's value."
So each time different object returned.

Please correct me if Im wrong..

Thanks!
Rohit.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into Integer.java source code, Integer.toString() will create a new String Object for each call. String.toString() will return you the same reference for each call.
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just need a quick tip as to where can i locate these Java classes on my machine (JDK installation) so that I can see the source code.
I seem to have lost the track of them.

Thanks!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look for a file src.zip in the jdk1.4.2 or jdk1.5.0 directory
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call Integer.toString() method, you are creating a new Object of type String. But when you call String.toString() the Java Virtual Machine looks for references identical already on the stack for optimization. So when you call String.toString() method you'll get a reference for the same object.

The same happens if you try:


String j = "Javier";
String j2 = "Javier";


if (j == j2) // This is true

If you wish to avoid this put:
String j = new String("Javier");
String j2 = new String("Javier");

if (j == j2) // This will be false

[ August 02, 2006: Message edited by: Javier Sanchez Cerrillo ]
[ August 02, 2006: Message edited by: Javier Sanchez Cerrillo ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Javier Sanchez Cerrillo:
. But when you call String.toString() the Java Virtual Machine looks for references identical already on the stack for optimization.



Sorry, no, this explanation is quite incorrect. The JVM does nothing special. It's simply how the implementation of java.lang.String.toString() is written:



No magic about it. Now, as to your next point:


The same happens if you try:

String j = "Javier";
String j2 = "Javier";



Again, this explanation is not right at all. It is simply not true that "the Java Virtual Machine looks for references identical already on the stack for optimization", under any circumstances, ever. The reason j and j2 are the same is because during class loading, identical literals are resolved to a single String instance. Nothing to do with the stack, nor with "looking" or "optimization" -- just how classes are loaded.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this done by the class loader?. Son can you explain if the for loop ocurs in at runtime this still prints out true. ???

public class Javier {
public static void main(String[] args) {

String[] j = new String[10];
for (int i = 0; i < j.length; i++) {
j[i] = "Javier";
}

System.out.println(j[0] == j[1]);
}
}

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

This happens at runtime.
[ August 02, 2006: Message edited by: Javier Sanchez Cerrillo ]
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


j[0] == j[1] because at compile time "Javier" is replaced by a reference to a String object from the String pool. Each time through the loop, that same reference is assigned to j[i].
 
reply
    Bookmark Topic Watch Topic
  • New Topic