• 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

please explain the concept behind this

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everybody.........

as i was going through wrapper classes, i have a doubt in one of the practise questions about the result.

public class Boxing4 {
public static void main(String[] args) {
Integer i = 10;
Integer j = 10;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}
the answer for the above one is true true,but the answer for the below one is false true,as i im expecting for the above one.

public class Boxing5 {
public static void main(String[] args) {
Integer i = 200;
Integer j = 200;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mukesh,

Even i am confused with the answers , but i tried with different values and found these output.



but when i use the value grater than 127 it behaves differently.


i think this will help to come to some solution or to make out why it gives such output.

thanks
Anvi
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I think this will give error.
Like :

yourfile.java:lineno : incompatible types
found : int
required : java.lang.Integer
Integer ..........
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is called autoboxing. Please refer to this link.Autoboxing

Similar problem discussed in the above link.

bye for now
sat
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

you are comparing 2 wrapper objects and not 2 primitives that have been unwrapped.

When 2 instances of Integer are created thru autoboxing, they are guaranted to be the same object (to save memory) so long as the number is in the range -128 to 127.

see chapt 3 of k&b (section boxing,== and equals())
if you can (Character behaves in a similiar fashion also).
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

But the range of the int is -2,147,483,648 to +2,147,483,648. I'll apreciate if any body clears my doubt.

thanks
Anvi
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is the range of int, but the only range where an int is guaranteed to be autoboxed to the same Integer object is -128 to 127.
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic