Boxing and expansion

What is the difference between the two. I know that Boxing converts primitive values ​​into a link. What is expanding. Should the sequence of the first box or expansion be done, or should it be?

+6
java
source share
6 answers

An extension converts a variable to another with a wider type.
Extension can be done using primitive or reference types.

For example:

String Object
int β†’ long

As stated in JLS:

conversion of boxing (Β§5.1.7) [is] optionally followed by extension of the reference conversion


Resources:

+8
source share
  • Extension wins boxing and var-args
  • Boxing wins over Var-Arts
  • The extension of the reference variable depends on inheritance (therefore, Integer cannot be expanded to Long. But Integer is expanded to a number).
  • Expansion and boxing impossible
  • Boxing and expansion possible
  • var-args can be combined with boxing or extension
+7
source share

Extension when assigned byte int . that is, you are expanding the data type.

The sequence should be boxing , then widening .

You CANNOT expand, then the field (int cannot be long).

You CAN , then expand (int can become an object via Integer)

Note. Highlighted words taken from Sun Certified Java Programmer SCJP 6 - Kathy Sierra

+3
source share
  • Boxing expansion, for example. go (int) will call go (long) instead of go (Integer) if both are available
  • The extension beats var-args, for example go (byte, byte), will call go (int, int) instead of the go (byte ... x) method.
  • Boxing defeats var-args, for example go (byte, byte) will call go (Byte, Byte) instead of the go (byte ... x) method. Expansion
  • Depends on the inheritance tree. For example. go (dog) can call go (Animal)
  • extension of the primitive shell is not possible, therefore go (Short) cannot call go (Integer), since they are not in the same inheritance hierarchy.
  • You CANNOT expand and then paste. For example. go (int) cannot call go (Long), because to call go (Long), the compiler needs to convert int to Integer, and then Integer to Long, which is impossible. (the rule mentioned above)
  • You can insert a field and then expand. For example. Int can be placed in the Integer field and then expanded to Object
+1
source share

An extension converts a primitive or non-primitive to a wider type (i.e. one that can contain more bytes).

Example:

 short -> int String -> Object 

But, int -> Integer does not expand; this is boxing. An extension has a higher priority than boxing. Also, both expansion and boxing cannot be performed together, i.e.

 int -> Long // cannot be done - both widening and boxing int -> long // can be done - only widening 
0
source share

I think the order is quite fascinating. I looked at the next site to see all the possible combinations. These are my functions:

 static void doSomeThing(short i) { System.out.println("short"); } static void doSomeThing(short... i) { System.out.println("short..."); } static void doSomeThing(Short i) { System.out.println("SHORT"); } static void doSomeThing(Short... i) { System.out.println("SHORT..."); } static void doSomeThing(long i) { System.out.println("long"); } static void doSomeThing(long... i) { System.out.println("long..."); } static void doSomeThing(Long i) { System.out.println("LONG"); } static void doSomeThing(Long... i) { System.out.println("LONG..."); } static void doSomeThing(int i) { System.out.println("int"); } static void doSomeThing(int... i) { System.out.println("int..."); } static void doSomeThing(Integer i) { System.out.println("INTEGER"); } static void doSomeThing(Integer... i) { System.out.println("INTEGER..."); } static void doSomeThing(Object i) { System.out.println("Object"); } static void doSomeThing(Object... i) { System.out.println("Object..."); } 

Rules:

  1.Searches for exactly the same type (int -> int) 2.Widening (int -> long) 3.Boxing (int-> Integer, it is NEVER possible to implicit box AND wide (int -> Long NOT possible without cast)) !!Multiple boxing go BEFORE var args!! int -> Object will be chosen before int -> int... 4.Var args (int -> int...) 5.Widening + var args (int -> long...) 6.Boxing + var args (int -> Integer...) 7.Boxing + widening + var args (int -> Object...) public class Main{ public static void main(String...args) { //primitive int int i = 0; doSomeThing(i); //int //commented out doSomeThing(int i){} doSomeThing(i); //long. It is not possible to narrow, so short, short... Short and Short... will NEVER be called when the input is larger than a short. //commented out doSomeThing(long i){} doSomeThing(i); //INTEGER //commented out doSomething(Integer i){} doSomeThing(i); //Object. Notice that there can be multiple boxing before moving to var args //Error occured: compiler if confused: can either execute int..., long..., Object... or Integer... //Object... and Integer... are commented out, because in the real world int... will be called first doSomeThing(i); //int... //commented out int... doSomeThing(i); //long... //commented out long... and uncommented Integer... doSomeThing(i); //Integer... //commented out Integer... and uncommented Object... doSomeThing(i); //Object... //Integer //Integer Integer i = new Integer(0); doSomeThing(i); //INTEGER //commented out doSomeThing(Integer i) doSomeThing(i); //Object //commented out doSomeThing(Object i) doSomeThing(i); //int //commented out doSomeThing(int i) doSomeThing(i); //long so NOT int... it goes widening again //commented out doSomeThing(long i) //Error occured: compliler refused: not both have int..., long..., Integer... and Object... //int... and long... are commented out doSomeThing(i); //INTEGER... //commented out doSomeThing(Integer... i) doSomeThing(i); //Object... //commented out doSomeThing(Object... i) //uncommented doSomeThing(int... and long...) doSomeThing(i); //int... //uncommented doSomeThing(int... i) doSomeThing(i); //long... } 
0
source share

All Articles