wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Autoboxing" Watch "Autoboxing" New topic
Author

Autoboxing

Breno Ribeiro
Greenhorn

Joined: Jan 27, 2012
Posts: 15
Hi, I don't know what is happen.

Look:
With Double (wraper), it doesn't work.



but the same code with primitive type (double), works.



What's the rule in this case?


Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

The rule for autoboxing is "primitive type is automatically encapsulted (boxed) into its equivalent type wrapper."

Autoboxing will work if you initialize intRoom variable with primitive double.

double intRoom = 2;

then pass value to book method and it will autoboxed from primitive double to Object Double.

Hope this helps.

MySQL Blog
http://mysqlearner.blogspot.com/
Breno Ribeiro
Greenhorn

Joined: Jan 27, 2012
Posts: 15
Zeeshan Sheikh wrote:The rule for autoboxing is "primitive type is automatically encapsulted (boxed) into its equivalent type wrapper."

Autoboxing will work if you initialize intRoom variable with primitive double.

double intRoom = 2;

then pass value to book method and it will autoboxed from primitive double to Object Double.

Hope this helps.


We have this rule, and other rule that says which a int fits into a double. So, if I have a I can do something like: Inclusive, if I have a double parameter, I can pass a int argument. So, why do I can't pass a int value to a Double (wraper) parameter? Understand?
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

I am sorry, I wasn't that clear in my previous post. Automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) . int is a primitive type and Wrapper classes are object. Object types act differently than primitive types.

I think you are talking about Widening Primitive Conversion which does not act as unboxing and boxing.

Forbidden Conversions: There is no permitted conversion from any primitive type to any reference type.

Hope this will help.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Breno Ribeiro wrote:


but the same code with primitive type (double), works.



public static void main(String[] args) {
short shortRoom = 1;
int intRoom = 2;

book(intRoom);
book(shortRoom);
}
}
[/code]




Hi, in this case, the compiler auto boxes intRoom into new Integer (2), but it cannot find a method with suitable argument type.

There is one thing the compiler won't do : widen the intRoom into double and boxes it into a Double object. Widening and boxing is too much for the compiler and it won't do it according to KB.

If you change your code like this:


This should work because the Integer intRoom is unboxed and passed to book(double a) method. The unboxed intRoom is an integer and it can be widened to double.

Breno Ribeiro
Greenhorn

Joined: Jan 27, 2012
Posts: 15
Helen Ma wrote:
Breno Ribeiro wrote:


but the same code with primitive type (double), works.



public static void main(String[] args) {
short shortRoom = 1;
int intRoom = 2;

book(intRoom);
book(shortRoom);
}
}
[/code]




Hi, in this case, the compiler auto boxes intRoom into new Integer (2), but it cannot find a method with suitable argument type.

There is one thing the compiler won't do : widen the intRoom into double and boxes it into a Double object. Widening and boxing is too much for the compiler and it won't do it according to KB.

If you change your code like this:


This should work because the Integer intRoom is unboxed and passed to book(double a) method. The unboxed intRoom is an integer and it can be widened to double.




It's true, you both are right!
Was difficult to find someone who given a easy explanation.

Thank's.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing