Boxing and Widening

前端 未结 7 1668
小蘑菇
小蘑菇 2020-12-30 08:22

What is the difference between these two. I know Boxing is converting primitive values to reference. What is widening. Also what should be the sequence first boxing should b

7条回答
  •  执笔经年
    2020-12-30 09:01

    I think the order is pretty fascinating. I made out the following playground to see every possible combination. This 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...
        }
    

提交回复
热议问题