• 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

wrapper classes

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()+";");
}
}
}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Veer Batra
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Hug your destiny! And hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic