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 wrapper classes 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 "wrapper classes" Watch "wrapper classes" New topic
Author

wrapper classes

abhishek chauhan
Greenhorn

Joined: Jul 21, 2005
Posts: 8
public class suncerti {
public static void main(String[] args) {
Integer a = new Integer(10);
Integer b = new Integer(10);
Long c = new Long(10);
if (a.toString() == b.toString()) {
System.out.println("true");
}
else {
System.out.println("false");
}
if (a.toString() == c.toString()) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}

why in this case the first if condition prints true and second false. the toString function returns a String object so in second case also we should have the SOP as true only
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi,

When u compare two wrapper objects of different classes it'll always return false.No matter even if it is converted into string.

Regards,
Priya.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Priya Jothi:
Hi,

When u compare two wrapper objects of different classes it'll always return false.No matter even if it is converted into string.

Regards,
Priya.


Hmm... I don't agree. Author compares references to String object but not a wrappers.


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Shaila Vijaya Raja
Greenhorn

Joined: Apr 09, 2005
Posts: 23
Hi abhishek,

In first case both the conversion results to a string of 10( "10" )which belongs to a same class, but not in the second case, eventhough the value is similar it belongs to a different class.


With Regards,<br />Shaila Vijaya Raja<br /> <br />SCJP1.4
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162

why in this case the first if condition prints true and second false. the toString function returns a String object so in second case also we should have the SOP as true only


As for me I wonder why first comparison returns "true". It seems like Integer pooling string representation of it's value.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
I check it on my JVM and the result is:
true
false

JVM:
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)


But one of my colleagues has result:
false
false

JVM:
java version "1.5.0_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)
[ July 27, 2005: Message edited by: George Bolyuba ]
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162


Hope this kind of question will not be on real exam.

(Code from java 1.4.2_08 sources )
[ July 27, 2005: Message edited by: George Bolyuba ]
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
For comparing Objects you should use .equals() method, not "==" operator. String is also an object, hence should be compared using .equals() method. Since String is immutable object, for performance reasons various JVM, re-use the string object if same string litteral is required, but you can not rely on that. Because of this you saw one true and one false.
If you modify the code as follows, it will work consistantly:

public class SychTest extends Thread implements Runnable
{
public static void main(String[] args)
{
Integer a= new Integer(10);
Integer b= new Integer(10);
Long c= new Long(10);
if (a.toString().equals(b.toString()))
{
System.out.println("true");
} else
{
System.out.println("false");
}
if (a.toString().equals(c.toString()))
{
System.out.println("true");
} else
{
System.out.println("false ;"+ c.toString()+";");
}
}
}


Devender Thareja
SCEA, SCBCD, SCJP
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
We must understand difference between == operator and .equals() method of Strings. == compares the references and .equals() method compares the contents of the references. When we create literal it is created in the pool and it can have one or many refrences as far as it is referring to same address refrence. e.g. when we say 'String s1= "Compare me"' it is assigned a place in the pool with reference s1 having value "Compare me". If we create another string like 'String s2= "Compare me"' still it looks in the pool of literal strings and if some reference has same value which is here "Compare me" then s2 string reference is also refered to the same value. So now value "Comapre me" is being referred by two refrences so references are same and values are same so when we use == or .equals() it succeeds and turns out to true.

Now take another example. Now we create an object say 'String s3=new String("Compare me")'. Whenever we construct a String calling constructor explicitely, it uses extra memory location without looking into the pool. So s3 is not the same refrence as s1 or s2 but has same value "Compare me". In this case if we want to compare refrences that is use == operator will turn out false but if we use .equals() method which will compare values, will turn out true. Here is the code which can prove that.


So coming back to original example, if we use constructor explicitely like 'Integer a = new Integer(10);' it will create extra memory location 'a' to store the value 10 and so on. If we use == operator it checks reference and should turn out to false but if we use .equals() method which compares value inside the location should turn out true. There s one method .intern() which can move the values from outside pool into literals pool and helps in reducing the memory requirements. Just one more thing to remember, even if one has same reference but if we convert to string it will create new memory location, so after converting to string == oprator, will give result always false. Here is the modified version of original example.



I hope this helps, correct me if I am wrong.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Veer Batra:

[CODE] public class suncerti1 {
public static void main(String[] args) {
Integer a = new Integer(10);
Integer b = new Integer(10);
Long c = new Long(10);
Integer a1 = 10;
Integer b1 = 10;

// converting to string will create new refrence and result will be false here

if (a.toString() == b.toString()) {
System.out.println("true");
}
else {
System.out.println("false");
}
g.


i dont' think this is right ..
please compare what u have written with original post..
here the answer is true false


Thanks and Regards, Amit Taneja
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Amit, Where you get true and false results. I am getting as under for all seven possibilities(see cut/paste of results):

false
false
true
true
true
false
true
Press any key to continue . . .

Which version of Java you are using? I am using 1.5. I am saying that because somebody mentioned here that results are different for 1.4 and 1.5.
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
I ran the following code using v 1.5 and v 1.3

public class StringTest{
public static void main(String r[]){
Integer u = new Integer(10);
Integer v = new Integer(10);
if(u == v){
System.out.println("true");
}
else{
System.out.println("false");
}
if(u.toString()==v.toString()){
System.out.println("true");
}
else{
System.out.println("false");
}
}//end of main
}//end of class

For both the JVMs the result was:
false
false

The only was to get true to print was to use equals. Which makes sense because we are comparing two different object and using "==". But I'm not sure why string pooling doesn't happen even when we are converting the Integer objects to String objects.

Fes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wrapper classes