Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain

 
kavitha satteli
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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
 
Vidhya Ramaswamy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Oggi Olli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic