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 “#”?

Make 24 using exactly three 3s

Where have Brexit voters gone?

How to cut a climbing rope?

Who decides how to classify a novel?

What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?

Have 1.5% of all nuclear reactors ever built melted down?

Can a US state pass/enforce a law stating that a group of people owe money to the state government?

Remove CiviCRM and Drupal links / banner on profile form

Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?

How to politely tell someone they did not hit "reply to all" in an email?

Question in discrete mathematics about group permutations

Why does the hash of infinity have the digits of π?

Why does this if-statement combining assignment and an equality check return true?

How to patch glass cuts in a bicycle tire?

Why do Russians almost not use verbs of possession akin to "have"?

Why aren't space telescopes put in GEO?

Is this statement about cut time correct?

Which European Languages are not Indo-European?

Is the field of q-series 'dead'?

Compaq Portable vs IBM 5155 Portable PC

Find the three digit Prime number P from the given unusual relationships

How to ignore kerning of underbrace in math mode

Dad jokes are fun

Is the Unsullied name meant to be ironic? How did it come to be?



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;








15















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


















15















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














15












15








15








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.9k1291123




37.9k1291123










asked May 11 at 17:37









JoeCrayonJoeCrayon

924




924







  • 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













4 Answers
4






active

oldest

votes


















20














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






























        -1














        The toString() of an Array returns [Array type@HashCode
        HashCode is a number that is calculated from the array.
        If you want to get an useful String you should use java.util.Arrays.toString(array) instead.



        For example



        System.out.println(new int[10].toString());
        System.out.println(java.util.Arrays.toString(new int[10]));


        resulted in



        [I@72d818d1
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]





        share|improve this answer























        • This doesn't answer the question.

          – ruohola
          May 19 at 19:31











        • Why? Because of this, the output is [I@140e19d and not 9

          – dan1st
          May 19 at 20:05












        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

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        20














        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



























          20














          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

























            20












            20








            20







            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,6443736




                    3,6443736





















                        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,95522136




                            6,95522136





















                                -1














                                The toString() of an Array returns [Array type@HashCode
                                HashCode is a number that is calculated from the array.
                                If you want to get an useful String you should use java.util.Arrays.toString(array) instead.



                                For example



                                System.out.println(new int[10].toString());
                                System.out.println(java.util.Arrays.toString(new int[10]));


                                resulted in



                                [I@72d818d1
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]





                                share|improve this answer























                                • This doesn't answer the question.

                                  – ruohola
                                  May 19 at 19:31











                                • Why? Because of this, the output is [I@140e19d and not 9

                                  – dan1st
                                  May 19 at 20:05
















                                -1














                                The toString() of an Array returns [Array type@HashCode
                                HashCode is a number that is calculated from the array.
                                If you want to get an useful String you should use java.util.Arrays.toString(array) instead.



                                For example



                                System.out.println(new int[10].toString());
                                System.out.println(java.util.Arrays.toString(new int[10]));


                                resulted in



                                [I@72d818d1
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]





                                share|improve this answer























                                • This doesn't answer the question.

                                  – ruohola
                                  May 19 at 19:31











                                • Why? Because of this, the output is [I@140e19d and not 9

                                  – dan1st
                                  May 19 at 20:05














                                -1












                                -1








                                -1







                                The toString() of an Array returns [Array type@HashCode
                                HashCode is a number that is calculated from the array.
                                If you want to get an useful String you should use java.util.Arrays.toString(array) instead.



                                For example



                                System.out.println(new int[10].toString());
                                System.out.println(java.util.Arrays.toString(new int[10]));


                                resulted in



                                [I@72d818d1
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]





                                share|improve this answer













                                The toString() of an Array returns [Array type@HashCode
                                HashCode is a number that is calculated from the array.
                                If you want to get an useful String you should use java.util.Arrays.toString(array) instead.



                                For example



                                System.out.println(new int[10].toString());
                                System.out.println(java.util.Arrays.toString(new int[10]));


                                resulted in



                                [I@72d818d1
                                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 16 at 8:57









                                dan1stdan1st

                                1958




                                1958












                                • This doesn't answer the question.

                                  – ruohola
                                  May 19 at 19:31











                                • Why? Because of this, the output is [I@140e19d and not 9

                                  – dan1st
                                  May 19 at 20:05


















                                • This doesn't answer the question.

                                  – ruohola
                                  May 19 at 19:31











                                • Why? Because of this, the output is [I@140e19d and not 9

                                  – dan1st
                                  May 19 at 20:05

















                                This doesn't answer the question.

                                – ruohola
                                May 19 at 19:31





                                This doesn't answer the question.

                                – ruohola
                                May 19 at 19:31













                                Why? Because of this, the output is [I@140e19d and not 9

                                – dan1st
                                May 19 at 20:05






                                Why? Because of this, the output is [I@140e19d and not 9

                                – dan1st
                                May 19 at 20:05


















                                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

                                Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                                Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                                What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company