• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Can anyone help me with the explanation of this output ???

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Integer i = -10;
Integer j = -10;
System.out.print(i==j);
System.out.print(i.equals(j));
Integer k = 128;
Integer l = 128;
System.out.print(k==l);
System.out.print(k.equals(l));
}

This gives the answer as

truetruefalsetrue

Can anyone help me in finding out why the third output statement gives false. But If we give the values inside the byte range (i.e. -128 to 127) then it gives true as the answer but I am pretty confused why the Integer object is taking the range as that of a byte.


 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup this a bit tricky

first understand this
== means that the values in the objects must be equal
equals() means that the objects are meaningfully equivalent

Two instances of the following Wrapper objects will always be == when their values are the same
Boolean
Byte
Character (from 0 to 127)
Short and Integer (from -128 to 127)

if the value for Short or Integer extends 127 then == evaluates to false but equals() is still true as the objects are meaningfully equivalent
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember


Two instances of the following wrapper objects will always be == when their primitive values are the same:

Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127



When you used the value 128, two separate objects are created that's why the answer is false
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,

Check out this Java Language link and see if it helps....


JLS
 
S. Vijay
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Faisal and Gaurav

Thanks for the explanation. I guessed that it took between -128 and 127 and for outside the range a new object is getting created.
Even now I am confused why even the Short and Integer class object is taking values only from -128 to 127 and outside which a new object is getting created. Do you know any kind of link for this so that i can have detailed explanation?

@ Sonali
I have gone through the link and the chapter assignment conversion but I couldn't find any information for this type of assignments.

Anyway thanks for your help guys.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure next time to check if the question has been already asked by someone else before posting it. This question has been addressed many times in multiple threads.

 
S. Vijay
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr. Mo

I referred to the previous posts. It gave me the answer
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But only if you use literals to assign to the wrapper objects (like true/false, 0, 1).
if you would use

then the output would be 'falsetrue', since 2 different objects are still involved.

You can see at as if the compiler replaces a small set of literals with a call to the static .valueOf method which uses cached values if possible.


 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed Rein. It a bit like the difference between thisand thisFor the exam you need to know how and why these are different.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijay,


In the JLS link there is lot of information about == and equals() method.Click on the link and click on Integer Literals and click on the first article.....and you reach here
Integer types and values and click on numerical equality operators...It also gives some information about == at the link:- Numerical Equality Operators == and != .
Please check the article 15.21.3 Reference Equality Operators == and !=
and then the last point it tells article about equals() also check that for more information. Equals method.This section consists of som sample program and some more information about the equals operator.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!