File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String.toString()  and Integer.toString() 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 "String.toString()  and Integer.toString()" Watch "String.toString()  and Integer.toString()" New topic
Author

String.toString() and Integer.toString()

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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???


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Rohit Nath
Ranch Hand

Joined: Jun 16, 2006
Posts: 387
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.


R.N
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Jun 16, 2006
Posts: 387
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!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Look for a file src.zip in the jdk1.4.2 or jdk1.5.0 directory


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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 ]

SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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 ]
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518


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].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String.toString() and Integer.toString()