• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Equals Method

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Invoke
{
public static void main(String[] args)
{
int i1=4000;
int i2=4000;

System.out.println(i1==i2);

new Invoke().go(i1,i2);
}

public void go(Integer i1, Integer i2)
{
System.out.println(i1==i2);
}
}

The answer is true false.
But when i1=2; i2=2; then answer is true.
Why so?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhi Vijay :


In case of go() method primitive int is auto-boxed to wrappered Integer.
It will create new objects, for new objects == will return false as their memory location would be different, whereas if you try .equals() method it will return true
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think,

: For ASCII value (-128 to 127), it will compare it as an value (so gives true).

: For other value, it will pass it as an object as shown in the code (So gives false).
 
Pranav Patel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the previous post, it is not readable..

i1=2, i2=2: For ASCII value (int -128 to 127), it will compare it as an int value (so gives true).
i1=4000, i2=4000: For other int value, it will pass it as an Integer object as shown in the code (So gives false).

Auto boxing for ASCII value for int works different here.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look code of Integer.valueOf() method in Integer class. You will get your answer.

If you call with i1=2, i2=2, then JVM will not going to create new Integer object. Why?

Here Integer.valueOf(2) method is get called not new Integer(2); And this method has already created a cache for Integer object of 256 size that will contain all Integer object starting from -128 to 127.
From -128 to 127, no new Integer objects are created, as they are already created and cached.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Punit and Pranav.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic