Why is this int array not passed as an object vararg array?What's the simplest way to print a Java array?Java arrays printing out weird numbers and textWhat is reflection and why is it useful?Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayWhat is a serialVersionUID and why should I use it?How do I convert a String to an int in Java?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why is printing “B” dramatically slower than printing “#”?

Why are there five extra turns in tournament Magic?

Why is the marginal distribution/marginal probability described as "marginal"?

Single word that parallels "Recent" when discussing the near future

Who is frowning in the sentence "Daisy looked at Tom frowning"?

Can a person still be an Orthodox Jew and believe that the Torah contains narratives that are not scientifically correct?

Divisor Rich and Poor Numbers

How could it be that 80% of townspeople were farmers during the Edo period in Japan?

Write electromagnetic field tensor in terms of four-vector potential

Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?

How does this piece of code determine array size without using sizeof( )?

Is there a method to separate iron from mercury?

What is the velocity distribution of the exhaust for a typical rocket engine?

Why use a retrograde orbit?

Do we see some Unsullied doing this in S08E05?

Iterate lines of string variable in bash

What color to choose as "danger" if the main color of my app is red

​Cuban​ ​Primes

Was the dragon prowess intentionally downplayed in S08E04?

How can we delete item permanently without storing in Recycle Bin?

Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?

How to know the path of a particular software?

He is the first man to arrive here

Working hours and productivity expectations for game artists and programmers

Why is the A380’s with-reversers stopping distance the same as its no-reversers stopping distance?



Why is this int array not passed as an object vararg array?


What's the simplest way to print a Java array?Java arrays printing out weird numbers and textWhat is reflection and why is it useful?Is Java “pass-by-reference” or “pass-by-value”?Create ArrayList from arrayWhat is a serialVersionUID and why should I use it?How do I convert a String to an int in Java?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is it faster to process a sorted array than an unsorted array?Why is printing “B” dramatically slower than printing “#”?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








12















I used this code. I am confused why this int array is not converted to an object vararg argument:



class MyClass 
static void print(Object... obj)
System.out.println("Object…: " + obj[0]);


public static void main(String[] args)
int[] array = new int[] 9, 1, 1;
print(array);
System.out.println(array instanceof Object);




I expected the output:



Object…: 9
true


but it gives:



Object…: [I@140e19d
true









share|improve this question



















  • 4





    The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

    – Zabuza
    May 11 at 18:27


















12















I used this code. I am confused why this int array is not converted to an object vararg argument:



class MyClass 
static void print(Object... obj)
System.out.println("Object…: " + obj[0]);


public static void main(String[] args)
int[] array = new int[] 9, 1, 1;
print(array);
System.out.println(array instanceof Object);




I expected the output:



Object…: 9
true


but it gives:



Object…: [I@140e19d
true









share|improve this question



















  • 4





    The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

    – Zabuza
    May 11 at 18:27














12












12








12








I used this code. I am confused why this int array is not converted to an object vararg argument:



class MyClass 
static void print(Object... obj)
System.out.println("Object…: " + obj[0]);


public static void main(String[] args)
int[] array = new int[] 9, 1, 1;
print(array);
System.out.println(array instanceof Object);




I expected the output:



Object…: 9
true


but it gives:



Object…: [I@140e19d
true









share|improve this question
















I used this code. I am confused why this int array is not converted to an object vararg argument:



class MyClass 
static void print(Object... obj)
System.out.println("Object…: " + obj[0]);


public static void main(String[] args)
int[] array = new int[] 9, 1, 1;
print(array);
System.out.println(array instanceof Object);




I expected the output:



Object…: 9
true


but it gives:



Object…: [I@140e19d
true






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 11 at 20:39









Boann

37.8k1291123




37.8k1291123










asked May 11 at 17:37









JoeCrayonJoeCrayon

774




774







  • 4





    The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

    – Zabuza
    May 11 at 18:27













  • 4





    The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

    – Zabuza
    May 11 at 18:27








4




4





The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

– Zabuza
May 11 at 18:27






The issue is that Object... means Object[] and int[] can not be converted to Object[] (int is a primitive, not an Object). However, the int[] array itself can be interpreted as Object. So you end up passing an object array Object[] with exactly one element in it, an int[]. So you have an array of arrays.

– Zabuza
May 11 at 18:27













3 Answers
3






active

oldest

votes


















21














You're running into an edge case where objects and primitives don't work as expected.
The problem is that the actual code ends up expecting static void print(Object[]), but int[] cannot be cast to Object[]. However it can be cast to Object, resulting in the following executed code: print(new int[][]array).



You get the behavior you expect by using an object-based array like Integer[] instead of int[].






share|improve this answer






























    8














    The reason for this is that an int array cannot be casted to an Object array implicitly. So you actually end up passing the int array as the first element of the Object array.



    You could get the expected output without changing your main method and without changing the parameters if you do it like this:



    static void print(Object... obj) 
    System.out.println("Object…: " + ((int[]) obj[0])[0]);




    Output:



    Object…: 9
    true






    share|improve this answer
































      0














      As you know, when we use varargs, we can pass one or more arguments separating by comma. In fact it is a simplification of array and the Java compiler considers it as an array of specified type.



      Oracle documentation told us that an array of objects or primitives is an object too:




      In the Java programming language, arrays are objects (§4.3.1), are
      dynamically created, and may be assigned to variables of type Object
      (§4.3.2). All methods of class Object may be invoked on an array.




      So when you pass an int[] to the print(Object... obj) method, you are passing an object as the first element of varargs, then System.out.println("Object…: " + obj[0]); prints its reference address (default toString() method of an object).






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56092777%2fwhy-is-this-int-array-not-passed-as-an-object-vararg-array%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        21














        You're running into an edge case where objects and primitives don't work as expected.
        The problem is that the actual code ends up expecting static void print(Object[]), but int[] cannot be cast to Object[]. However it can be cast to Object, resulting in the following executed code: print(new int[][]array).



        You get the behavior you expect by using an object-based array like Integer[] instead of int[].






        share|improve this answer



























          21














          You're running into an edge case where objects and primitives don't work as expected.
          The problem is that the actual code ends up expecting static void print(Object[]), but int[] cannot be cast to Object[]. However it can be cast to Object, resulting in the following executed code: print(new int[][]array).



          You get the behavior you expect by using an object-based array like Integer[] instead of int[].






          share|improve this answer

























            21












            21








            21







            You're running into an edge case where objects and primitives don't work as expected.
            The problem is that the actual code ends up expecting static void print(Object[]), but int[] cannot be cast to Object[]. However it can be cast to Object, resulting in the following executed code: print(new int[][]array).



            You get the behavior you expect by using an object-based array like Integer[] instead of int[].






            share|improve this answer













            You're running into an edge case where objects and primitives don't work as expected.
            The problem is that the actual code ends up expecting static void print(Object[]), but int[] cannot be cast to Object[]. However it can be cast to Object, resulting in the following executed code: print(new int[][]array).



            You get the behavior you expect by using an object-based array like Integer[] instead of int[].







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 11 at 17:42









            KiskaeKiskae

            14.3k13244




            14.3k13244























                8














                The reason for this is that an int array cannot be casted to an Object array implicitly. So you actually end up passing the int array as the first element of the Object array.



                You could get the expected output without changing your main method and without changing the parameters if you do it like this:



                static void print(Object... obj) 
                System.out.println("Object…: " + ((int[]) obj[0])[0]);




                Output:



                Object…: 9
                true






                share|improve this answer





























                  8














                  The reason for this is that an int array cannot be casted to an Object array implicitly. So you actually end up passing the int array as the first element of the Object array.



                  You could get the expected output without changing your main method and without changing the parameters if you do it like this:



                  static void print(Object... obj) 
                  System.out.println("Object…: " + ((int[]) obj[0])[0]);




                  Output:



                  Object…: 9
                  true






                  share|improve this answer



























                    8












                    8








                    8







                    The reason for this is that an int array cannot be casted to an Object array implicitly. So you actually end up passing the int array as the first element of the Object array.



                    You could get the expected output without changing your main method and without changing the parameters if you do it like this:



                    static void print(Object... obj) 
                    System.out.println("Object…: " + ((int[]) obj[0])[0]);




                    Output:



                    Object…: 9
                    true






                    share|improve this answer















                    The reason for this is that an int array cannot be casted to an Object array implicitly. So you actually end up passing the int array as the first element of the Object array.



                    You could get the expected output without changing your main method and without changing the parameters if you do it like this:



                    static void print(Object... obj) 
                    System.out.println("Object…: " + ((int[]) obj[0])[0]);




                    Output:



                    Object…: 9
                    true







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 11 at 18:30

























                    answered May 11 at 17:39









                    ruoholaruohola

                    3,4142635




                    3,4142635





















                        0














                        As you know, when we use varargs, we can pass one or more arguments separating by comma. In fact it is a simplification of array and the Java compiler considers it as an array of specified type.



                        Oracle documentation told us that an array of objects or primitives is an object too:




                        In the Java programming language, arrays are objects (§4.3.1), are
                        dynamically created, and may be assigned to variables of type Object
                        (§4.3.2). All methods of class Object may be invoked on an array.




                        So when you pass an int[] to the print(Object... obj) method, you are passing an object as the first element of varargs, then System.out.println("Object…: " + obj[0]); prints its reference address (default toString() method of an object).






                        share|improve this answer



























                          0














                          As you know, when we use varargs, we can pass one or more arguments separating by comma. In fact it is a simplification of array and the Java compiler considers it as an array of specified type.



                          Oracle documentation told us that an array of objects or primitives is an object too:




                          In the Java programming language, arrays are objects (§4.3.1), are
                          dynamically created, and may be assigned to variables of type Object
                          (§4.3.2). All methods of class Object may be invoked on an array.




                          So when you pass an int[] to the print(Object... obj) method, you are passing an object as the first element of varargs, then System.out.println("Object…: " + obj[0]); prints its reference address (default toString() method of an object).






                          share|improve this answer

























                            0












                            0








                            0







                            As you know, when we use varargs, we can pass one or more arguments separating by comma. In fact it is a simplification of array and the Java compiler considers it as an array of specified type.



                            Oracle documentation told us that an array of objects or primitives is an object too:




                            In the Java programming language, arrays are objects (§4.3.1), are
                            dynamically created, and may be assigned to variables of type Object
                            (§4.3.2). All methods of class Object may be invoked on an array.




                            So when you pass an int[] to the print(Object... obj) method, you are passing an object as the first element of varargs, then System.out.println("Object…: " + obj[0]); prints its reference address (default toString() method of an object).






                            share|improve this answer













                            As you know, when we use varargs, we can pass one or more arguments separating by comma. In fact it is a simplification of array and the Java compiler considers it as an array of specified type.



                            Oracle documentation told us that an array of objects or primitives is an object too:




                            In the Java programming language, arrays are objects (§4.3.1), are
                            dynamically created, and may be assigned to variables of type Object
                            (§4.3.2). All methods of class Object may be invoked on an array.




                            So when you pass an int[] to the print(Object... obj) method, you are passing an object as the first element of varargs, then System.out.println("Object…: " + obj[0]); prints its reference address (default toString() method of an object).







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 12 at 4:59









                            aminographyaminography

                            6,91522136




                            6,91522136



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56092777%2fwhy-is-this-int-array-not-passed-as-an-object-vararg-array%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                                Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?