• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boxing then widening  RSS feed

 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Im trying to get my head around the subject of autoboxing.
I have seen the 5 golden rules, and have tried to find suitable examples to demonstrate them, as such.


1. Primitive Widening > Boxing > Varargs.
2. Widening and Boxing (WB) not allowed.
3. Boxing and Widening (BW) allowed.
4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner i.e. not together.
5. Widening between wrapper classes not allowed

Please feel free to add, but in particular, Im trying to under the rules surrounding 2 & 3.

I just wondered whether my exmaple below for number 3 is valid or not, Im just wondering when they talk about widening, is this something that can only be done on primitive types, so you couldn't, for example widen an Integer to a Long, but you can with a int to a long. ??

Is this a valid BOXING and WIDENING example:


Thanks for your inputs, would be good to see some samples for all 5 scenarios if you have a spare moment to contribute. Thank you again.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I think you are going to have to revise your rules the hard way: To the Batmobile! Java Language Specification.
It is worth putting that code inside a method and seeing whether it compiles. I think it will but am not certain.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!