Forums Register Login

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

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.

1
+Pie Number of slices to send: Send
 

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").
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 626 times.
Similar Threads
Primitive and unboxing
Not sure why my answer on overloading was incorrect
Overloading with var-args
Advanced Overloading (widening,boxing,var args)
Passing null references
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:29:39.