Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive data type and Objet Data Type query Edited question from KB 6 Book

 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below code is edited version of KB 6 chapter 2 test q no 15.

I have a question about Primitive and object type.
As per i know since college days that Primitive and Object are different in java.

Primitive values are not Object in Java. [ My understanding may be Wrong please correct me if i am wrong ]

Following is Code :


Explanation in KB 6 Book :
an int can be boxed to an Integer and then "widened" to an Object

But i don't understand this explanation in context of this method invocation.


Please Help me to clear this point.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you have a couple of different concepts involved there.

The first is "auto-boxing". Do you understand that? Because you can't use primitives where you need an Object (such as in collections) all primitive types have a corresponding wrapper class. So int has Integer, double has Double, etc.

Which means that Java code used to be full of conversions between primitives and wrappers.
(in fact, it was worse than that, because you didn't have generics either, so there was an added cast to Integer).

Auto-boxing was added to the language to make life easier - these conversions can be carried out automatically:


So if you have a method that takes an Integer argument, you can call it with an int argument and the auto-boxing takes care of the rest.

The second thing going on with your code is that an Integer is an Object. Which means that if you have a method that takes an Object argument, you can always give it an Integer argument instead (the "substitution principle"). Put these together, and you can also call it with an int argument. This will "box then widen" - convert an int to an Integer, then use that as an Object.

One thing to remember, though, is that you cannot "widen then box". For example, if you tried this:
That won't work. The int can be boxed to an Integer, but then that can't isn't compatible with a Long. And the compiler will not convert the int to a long and then box that to a Long ("widen then box"). So that won't compile.
 
Sergei Zhylinski
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is an autoboxing and relates only to wrapper objects. So the java itself will provide necessary changes. I suppose these is a compiler who will insert necessary byte-code into your program at compile time. When you pass a primitive to your method, and object of type 'Integer' will be created (since the default for numerics is int). This object will be passed to the method. And next step is a simple reference casting, implicit in our case.

Regards,
Sergei Zhylinski
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't add anything useful to Matthew's excellent explanation

So I'll just stick to this (administrative) note: autoboxing/unboxing is not on the OCAJP7 exam (it is a topic for the more advanced OCPJP7 exam).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:So I'll just stick to this (administrative) note: autoboxing/unboxing is not on the OCAJP7 exam (it is a topic for the more advanced OCPJP7 exam).

Questions about it aren't. But questions containing code like this are:
list.add(4);

In other words, reading code that uses autoboxing could come up. But you wouldn't need to know anything subtle about it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:But you wouldn't need to know anything subtle about it.

And that's the most import thing, because there are nonetheless a few subtleties
 
aditya chaudhari
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew for detailed explanation and for one Extra point you teach me ,

The int can be boxed to an Integer, but then that can't isn't compatible with a Long. And the compiler will not convert the int to a long and then box that to a Long ("widen then box"). So that won't compile.


Thank you Sergei Zhylinski for one catchy point as
(since the default for numerics is int)
i 'll experiment on this.


Roel ...I spend a half day on this and come across your post
So I'll just stick to this (administrative) note: autoboxing/unboxing is not on the OCAJP7 exam (it is a topic for the more advanced OCPJP7 exam).
.
Thanks Roel for keeping eye on each small topic we all guys post here and its relevance with Exam Objectives.

Its better just to be aware of autoboxing/unboxing.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aditya chaudhari wrote:Its better just to be aware of autoboxing/unboxing.

That's true! Otherwise code like arrayList.add(40); maybe puts you off resulting in a wrong answer (e.g. "compilation fails").
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic