File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain" Watch "please explain" New topic
Author

please explain

kavitha satteli
Greenhorn

Joined: Jul 31, 2007
Posts: 9
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

Joined: May 23, 2007
Posts: 131
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


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
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

Joined: May 23, 2007
Posts: 131
PS:
supplement--above informations from K&B book.
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
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

Joined: Jun 04, 2007
Posts: 74
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.


SCJP 5.0, SCWCD in progress
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
Hi Vidhya Ramaswamy!with pleasure.
that at chap3 and page64 in the K&B book.
Vidhya Ramaswamy
Ranch Hand

Joined: Oct 10, 2007
Posts: 65
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

Joined: Apr 20, 2006
Posts: 3344

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!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
kavitha satteli
Greenhorn

Joined: Jul 31, 2007
Posts: 9
thanks Jia , actually iam preparing fo scjp1.5 but idont have proper mock exams can you please provide the same
Oggi Olli
Ranch Hand

Joined: Oct 11, 2007
Posts: 83
Seems like it works in the same way as Strings when value is in range -127 to 128. Otherwise not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain
 
Similar Threads
comparison involving primitive and wrapper
Question on ==, equals()
please explain the concept behind this
doubt about boxing conversions
Is the following output correct ?