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 all the squares explode

Why doesn't Rocket Lab use a solid stage?

Can I make ravioli dough with only all-purpose flour or do I NEED semolina flour?

Word for being out at night during curfew

Why didn't Aeroflot Flight 1492 dump its fuel first?

Why was Thor doubtful about his worthiness to Mjolnir?

The CompuTaria Quest - A young Wizard's journey

List software from restricted, multiverse separately

Is there any evidence to support the claim that the United States was "suckered into WW1" by Zionists, made by Benjamin Freedman in his 1961 speech?

Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?

Can 'sudo apt-get remove [write]' destroy my Ubuntu?

Why was the Ancient One so hesitant to teach Dr. Strange the art of sorcery?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

How to compact two the parabol commands in the following example?

Usefulness of complex chord names?

Why was castling bad for white in this game, and engine strongly prefered trading queens?

Was this a power play by Daenerys?

Why can't RGB or bicolour LEDs produce a decent yellow?

Exclude loop* snap devices from lsblk output?

Extracting sublists that contain similar elements

Speculative Biology of a Haplodiploid Humanoid Species

Ex-manager wants to stay in touch, I don't want to

Extrude the faces of a cube symmetrically along XYZ

"Right on the tip of my tongue" meaning?



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;








7















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



















  • 2





    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
    2 hours ago


















7















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



















  • 2





    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
    2 hours ago














7












7








7








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 42 mins ago









Boann

37.8k1291123




37.8k1291123










asked 3 hours ago









JoeCrayonJoeCrayon

492




492







  • 2





    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
    2 hours ago













  • 2





    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
    2 hours ago








2




2





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
2 hours ago






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
2 hours ago













2 Answers
2






active

oldest

votes


















10














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






























    5














    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

























      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

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      10














      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



























        10














        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

























          10












          10








          10







          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 3 hours ago









          KiskaeKiskae

          14.1k13243




          14.1k13243























              5














              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





























                5














                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



























                  5












                  5








                  5







                  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 2 hours ago

























                  answered 3 hours ago









                  ruoholaruohola

                  3,1902634




                  3,1902634



























                      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