Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

== , 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.
 
and POOF! You're gone! But look, this tiny ad is still here:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic