• 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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
}
}

output for the above code is falsetrue
public class Boxing5 {
public static void main(String[] args) {
Integer i = 20;
Integer j = 20;
System.out.print(i==j);
System.out.print(i.equals(j));
}
}

but o/p for the above code is truetrue
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
that is a good question.

Java in order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same:
1,Boolean
2,Byte
3,Character. from \u0000 to \u007f (7f is 127 in decimal)
4,Short and Integer. from -128 to 127
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really strange. I tried to work around with different number sets and found that it returns 'truetrue' until i and j are within the range
-128 to 127. The moment they are given the values 128 or higher(or -129 or lower), the result is 'falsetrue'.

Can anyone explain, why this behaviour and is it something to do with the way bytes are stored in Java?
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS:
supplement--above informations from K&B book.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jia,

Could you please give more info- which chapter/pg no in K&B book, if you have it handy?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While boxing, if the value assigned to Integer object is upto +128, object behaves as a interned object (i.e. a new object with same value wont be created but the new reference will be pointed to the previous existing object).
Example -

Integer a = 20;
Integer b = 20;

Since 20 is less than 128, reference b will point to same object referenced by a. That is why a==b gives true.
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vidhya Ramaswamy!with pleasure.
that at chap3 and page64 in the K&B book.
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mohit and Jia for the replies. Interesting theory behind autoboxing.
I have only finished 2 chapters in K&B book and intend to start chap 3.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats great.

I remember there used to be a couple of questions with respect to GC and how many objects are eligible for GC which includes this concept behind the scene!
 
kavitha satteli
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Jia , actually iam preparing fo scjp1.5 but idont have proper mock exams can you please provide the same
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like it works in the same way as Strings when value is in range -127 to 128. Otherwise not.
reply
    Bookmark Topic Watch Topic
  • New Topic