• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

== , equals() method problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Vars {
public static void main(String args[]) {
Integer i1 = 123;
Integer i2 = 123;
Integer i3 = 12345;
Integer i4 = 12345;
int i5 = 123;
System.out.println(i1==i2);
System.out.println(i3==i4);
System.out.println(i3<=i4);
System.out.println(i3.equals(i4));
System.out.println(i1==i5);
System.out.println(i1>i4);
}
}

output is:
true
false
true
true
true
false
Why second one is false bott references are identical though false I unable to getting this question given JavaBeat beta question anybody expedite.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The Integer,Short and Long shows true upto 127 (Maximum value of byte)
Once it exceed it shows false.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check marc weber clearing this up in
Boxing == Long


Regards,
Teo
[ October 17, 2007: Message edited by: Teo Framoe ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To save memory, java doesn�t create new object for any of the following values; it does mean that the values within this rages are referred to by same object. It is very similar to String object where two similar string objects is referred by only one object in the String pool.

Value ranges are as follows;
int, short -128 to 127
boolean true or false
char \u0000 to \u007f
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good as I explained with the greatest wrapper to receive is true with 127 above that will always be false. Unless you use the method equals (). Now I do not understand why the line below is true? This result is a rule or was a coincidence?


thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a coincidence.
i3 an i4 both exceeded 127 so objects reference will be used
to compare instead of the value.

Sometimes it may return false also. Try creating object i4 before i3 and then compare.
 
reply
    Bookmark Topic Watch Topic
  • New Topic