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

Autoboxing

 
Breno Ribeiro
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Breno Ribeiro
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic