I am confused i.toString==i.toString
Hello,
I was writing programs to get an understanding of the java certificate exam.
I am not sure this is related but can anyone explain me why the follwing is happening?
It is late and my brain is pickled inside my head.
--first one--jdk1.3.1 (Jbuilder 7)
public class test {
public test() {
}
public static void main(String[] args) {
String[] aas = { "array", "of", "String", };
test test1 = new test();
Integer i=new Integer(12);
System.out.println("i==i:"+(i.toString()==i.toString()));
Integer i1=new Integer(10);
Integer i2=new Integer("10");
System.out.println("i1==i2?:"+(i1.toString()==i2.toString()));
System.out.println("i1==i1?:"+(i1.toString()==i1.toString()));
Integer i3=new Integer(12);
System.out.println("i==i:"+(i3.toString()==i3.toString()));
}
}
--Output of this program
D:\JB7\jdk1.3.1\bin\javaw -classpath............ test
i==i:false
i1==i2?:false
i1==i1?:false
i==i:false
--second programjdk1.4.2-
public class str{
public static void main(String args[]){
Integer i1=new Integer(10);
Integer i2=new Integer("10");
System.out.println("i1==i2?:"+(i1.toString()==i2.toString()));
System.out.println("i1==i1?:"+(i1.toString()==i1.toString()));
Integer i3=new Integer(12);
System.out.println("i==i:"+(i3.toString()==i3.toString()));
}
}
output:-
C:\proj\cert>java str
i1==i2?:true
i1==i1?:true
i==i:false
my problem was about the differences between outputs.
jdk1.3.1 i.toString()==i.toString() is false
but in
jdk1.4.2 i.toString()==i.toString() is true.
is this a bug or a memory management difference?
what is the answer for
i.toString()==i.toString()
is it always false? so why in jdk1.4.2 it is true?
king regards
baris
all right i tried it again
public class str in both jdk1.4.2 and jdk1.3.1
here are the results
output1
C:\proj\str>notepad str.java
C:\proj\str>javac str.java
C:\proj\str>java str
i1==i2?:true
i1==i1?:true
i==i:false
C:\proj\str>java -version
java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)
C:\proj\str>
-Output2-
D:\JB7\jdk1.3.1\bin\javaw -classpath "C:\Documents and Settings\Administrator\jbproject\test\classes;D:\JB7\lib\xerces.jar;D:\JB7\jdk1.3.1\demo\jfc\Java2D\Java2Demo.jar;D:\JB7\jdk1.3.1\jre\lib\i18n.jar;D:\JB7\jdk1.3.1\jre\lib\jaws.jar;D:\JB7\jdk1.3.1\jre\lib\rt.jar;D:\JB7\jdk1.3.1\jre\lib\sunrsasign.jar;D:\JB7\jdk1.3.1\lib\dt.jar;D:\JB7\jdk1.3.1\lib\htmlconverter.jar;D:\JB7\jdk1.3.1\lib\tools.jar" str
i1==i2?:false
i1==i1?:false
i==i:false
--
no change...... results are different.
Hi,
I would expect this behaviour. The toString() method simply returns a String (or, more accurately, a String object). Two String objects may well have the same value, but == will return false, because it only tests object identity, it does not test the 'value' of an object.
It would appear that in one version of the jdk Integer.toString() creates only a single String object for each Integer object and returns that object for each subsequent toString() call, and that in another version it creates a new String object for every call to toString(). Both are acceptable ways of implementing toString(). Your test is simply highlighting an implementation detail.
You should really be using the equals() method, i.e.
i.toString().equals( i.toString() )
if you actually want to compare the Strings, as opposed to the String objects.
If you really want to use object equality then you will need to use the String.intern() method:
i.toString().intern() == i.toString().intern()
which makes guarantees about String objects. Both of these comparisons should always return true.
Ol.
0sa at 2007-7-16 3:12:22 >
![top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...](/library/images/10/top.gif)