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

The CompuTaria Quest - A young Wizard's journey

Reaction of borax with NaOH

Why was this sacrifice sufficient?

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

How can this pool heater gas line be disconnected?

Does the 500 feet falling cap apply per fall, or per turn?

Getting a wrong output using arraylists

Find all edge self-avoiding path of a graph

On studying Computer Science vs. Software Engineering to become a proficient coder

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

How to send a packet across NAT to local machine

Was there ever any real use for a 6800-based Apple I?

What is the significance of 4200 BCE in context of farming replacing foraging in Europe?

How did Thanos not realise this had happened at the end of Endgame?

What is Plautus’s pun about frustum and frustrum?

Do atomic orbitals "pulse" in time?

Speculative Biology of a Haplodiploid Humanoid Species

Drawing lines to nearest point

Why is “Ich wusste, dass aus dir mal was wird” grammitally correct?

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

Was this character’s old age look CGI or make-up?

Exception propagation: When should I catch exceptions?

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?

How are Core iX names like Core i5, i7 related to Haswell, Ivy Bridge?



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;








6















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



















  • 1





    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


















6















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



















  • 1





    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














6












6








6








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









Boann

37.8k1291123




37.8k1291123










asked 3 hours ago









JoeCrayonJoeCrayon

442




442







  • 1





    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













  • 1





    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








1




1





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


















8














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






























    4














    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









      8














      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














        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












          8








          8







          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.1k13242




          14.1k13242























              4














              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





























                4














                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



























                  4












                  4








                  4







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




                  3,1802634



























                      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

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480