Hi all,
consider below code:
public class Bertha {

    static String s = "";
    public static void main(String[] args) {
        int x=4;
        Boolean y = true;
        short[] sa = {1,2,3};
        doStuff(x,y);
        doStuff(x);
        doStuff(sa,sa);
        System.out.println(s);
    }
    static void doStuff(Object o){
        s += "1";       
    }
    static void doStuff(Object... o){
        s += "2";
    }
    static void doStuff(Integer... i){
        s += "3";
    }
    static void doStuff(Long L){
        s += "4";
    }
}

output : 212

here i have doubt that how Object... o have been called for doStuff(x,y) and for doStuff(sa,sa);

could anyone explain what goes around here.

Thanks in advance.

Recommended Answers

All 2 Replies

an array in Java is an Object. not an Integer, but still an Object.
so, you pass two instances of Object to doStuff, and the only method you have capable of dealing with this is:

static void doStuff(Object... o){
s += "2";
}

... the doStuff(x,y) case is a little more interesting:
because there's no method that takes an int and a boolean, the best fit the compiler can find is to auto-box the int into and Integer object and the boolean into a Boolean object, then use the Object... method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.