How to see if an object is an array without using reflection?










78














How can I see in Java if an Object is an array without using reflection?
And how can I iterate through all items without using reflection?



I use Google GWT so I am not allowed to use reflection :(



I would love to implement the following methods without using refelection:



private boolean isArray(final Object obj) 
//??..


private String toString(final Object arrayObject)
//??..



BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments.










share|improve this question




























    78














    How can I see in Java if an Object is an array without using reflection?
    And how can I iterate through all items without using reflection?



    I use Google GWT so I am not allowed to use reflection :(



    I would love to implement the following methods without using refelection:



    private boolean isArray(final Object obj) 
    //??..


    private String toString(final Object arrayObject)
    //??..



    BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments.










    share|improve this question


























      78












      78








      78


      9





      How can I see in Java if an Object is an array without using reflection?
      And how can I iterate through all items without using reflection?



      I use Google GWT so I am not allowed to use reflection :(



      I would love to implement the following methods without using refelection:



      private boolean isArray(final Object obj) 
      //??..


      private String toString(final Object arrayObject)
      //??..



      BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments.










      share|improve this question















      How can I see in Java if an Object is an array without using reflection?
      And how can I iterate through all items without using reflection?



      I use Google GWT so I am not allowed to use reflection :(



      I would love to implement the following methods without using refelection:



      private boolean isArray(final Object obj) 
      //??..


      private String toString(final Object arrayObject)
      //??..



      BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments.







      java arrays gwt instanceof






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 29 '14 at 13:44









      bluish

      13.8k1693147




      13.8k1693147










      asked Apr 27 '10 at 22:10









      edbrasedbras

      1,39252652




      1,39252652






















          4 Answers
          4






          active

          oldest

          votes


















          210














          You can use Class.isArray()



          public static boolean isArray(Object obj)

          return obj!=null && obj.getClass().isArray();



          This works for both object and primitive type arrays.



          For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.






          share|improve this answer






























            61














            You can use instanceof.



            JLS 15.20.2 Type Comparison Operator instanceof




             RelationalExpression:
            RelationalExpression instanceof ReferenceType


            At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.




            That means you can do something like this:



            Object o = new int 1,2 ;
            System.out.println(o instanceof int); // prints "true"


            You'd have to check if the object is an instanceof boolean, byte, short, char, int, long, float, double, or Object, if you want to detect all array types.



            Also, an int is an instanceof Object, so depending on how you want to handle nested arrays, it can get complicated.



            For the toString, java.util.Arrays has a toString(int) and other overloads you can use. It also has deepToString(Object) for nested arrays.



            public String toString(Object arr) 
            if (arr instanceof int)
            return Arrays.toString((int) arr);
            else //...



            It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.



            See also



            • Managing highly repetitive code and documentation in Java

            • Java Arrays.equals() returns false for two dimensional arrays.





            share|improve this answer






















            • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
              – edbras
              Apr 28 '10 at 7:44






            • 2




              BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
              – edbras
              Apr 28 '10 at 8:35










            • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
              – polygenelubricants
              Apr 28 '10 at 8:38










            • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
              – edbras
              Apr 28 '10 at 11:05










            • @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
              – polygenelubricants
              Apr 28 '10 at 11:16


















            28














            One can access each element of an array separately using the following code:



            Object o=...;
            if ( o.getClass().isArray() )
            for(int i=0; i<Array.getLength(o); i++)
            System.out.println(Array.get(o, i));




            Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.






            share|improve this answer


















            • 2




              isArray() was already adequately covered in the answers posted 4 years prior to this one.
              – Jason C
              Nov 17 '14 at 4:47






            • 13




              This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
              – Christopher Yang
              Jan 13 '15 at 5:40











            • @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
              – Stephen C
              Nov 12 '18 at 23:59


















            9














            There is no subtyping relationship between arrays of primitive type, or between an array of a primitive type and array of a reference type. See JLS 4.10.3.



            Therefore, the following is incorrect as a test to see if obj is an array of any kind:



            // INCORRECT!
            public boolean isArray(final Object obj)
            return obj instanceof Object;



            Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)




            I use Google GWT so I am not allowed to use reflection :(




            The best solution (to the isArray array part of the question) depends on what counts as "using reflection".



            • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.



            • Otherwise, the best way of figuring out whether an object has an array type is to use a sequence of instanceof expressions.



              public boolean isArray(final Object obj) 
              obj instanceof byte



            • You could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.



              public boolean isArray(final Object obj) 
              return obj.getClass().toString().charAt(0) == '[';




            1 - More precisely, the Class.isArray method is listed as supported by GWT in this page.






            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%2f2725533%2fhow-to-see-if-an-object-is-an-array-without-using-reflection%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









              210














              You can use Class.isArray()



              public static boolean isArray(Object obj)

              return obj!=null && obj.getClass().isArray();



              This works for both object and primitive type arrays.



              For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.






              share|improve this answer



























                210














                You can use Class.isArray()



                public static boolean isArray(Object obj)

                return obj!=null && obj.getClass().isArray();



                This works for both object and primitive type arrays.



                For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.






                share|improve this answer

























                  210












                  210








                  210






                  You can use Class.isArray()



                  public static boolean isArray(Object obj)

                  return obj!=null && obj.getClass().isArray();



                  This works for both object and primitive type arrays.



                  For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.






                  share|improve this answer














                  You can use Class.isArray()



                  public static boolean isArray(Object obj)

                  return obj!=null && obj.getClass().isArray();



                  This works for both object and primitive type arrays.



                  For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 27 '10 at 23:49

























                  answered Apr 27 '10 at 23:26









                  Steve KuoSteve Kuo

                  31.9k69176235




                  31.9k69176235























                      61














                      You can use instanceof.



                      JLS 15.20.2 Type Comparison Operator instanceof




                       RelationalExpression:
                      RelationalExpression instanceof ReferenceType


                      At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.




                      That means you can do something like this:



                      Object o = new int 1,2 ;
                      System.out.println(o instanceof int); // prints "true"


                      You'd have to check if the object is an instanceof boolean, byte, short, char, int, long, float, double, or Object, if you want to detect all array types.



                      Also, an int is an instanceof Object, so depending on how you want to handle nested arrays, it can get complicated.



                      For the toString, java.util.Arrays has a toString(int) and other overloads you can use. It also has deepToString(Object) for nested arrays.



                      public String toString(Object arr) 
                      if (arr instanceof int)
                      return Arrays.toString((int) arr);
                      else //...



                      It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.



                      See also



                      • Managing highly repetitive code and documentation in Java

                      • Java Arrays.equals() returns false for two dimensional arrays.





                      share|improve this answer






















                      • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                        – edbras
                        Apr 28 '10 at 7:44






                      • 2




                        BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                        – edbras
                        Apr 28 '10 at 8:35










                      • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                        – polygenelubricants
                        Apr 28 '10 at 8:38










                      • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                        – edbras
                        Apr 28 '10 at 11:05










                      • @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                        – polygenelubricants
                        Apr 28 '10 at 11:16















                      61














                      You can use instanceof.



                      JLS 15.20.2 Type Comparison Operator instanceof




                       RelationalExpression:
                      RelationalExpression instanceof ReferenceType


                      At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.




                      That means you can do something like this:



                      Object o = new int 1,2 ;
                      System.out.println(o instanceof int); // prints "true"


                      You'd have to check if the object is an instanceof boolean, byte, short, char, int, long, float, double, or Object, if you want to detect all array types.



                      Also, an int is an instanceof Object, so depending on how you want to handle nested arrays, it can get complicated.



                      For the toString, java.util.Arrays has a toString(int) and other overloads you can use. It also has deepToString(Object) for nested arrays.



                      public String toString(Object arr) 
                      if (arr instanceof int)
                      return Arrays.toString((int) arr);
                      else //...



                      It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.



                      See also



                      • Managing highly repetitive code and documentation in Java

                      • Java Arrays.equals() returns false for two dimensional arrays.





                      share|improve this answer






















                      • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                        – edbras
                        Apr 28 '10 at 7:44






                      • 2




                        BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                        – edbras
                        Apr 28 '10 at 8:35










                      • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                        – polygenelubricants
                        Apr 28 '10 at 8:38










                      • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                        – edbras
                        Apr 28 '10 at 11:05










                      • @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                        – polygenelubricants
                        Apr 28 '10 at 11:16













                      61












                      61








                      61






                      You can use instanceof.



                      JLS 15.20.2 Type Comparison Operator instanceof




                       RelationalExpression:
                      RelationalExpression instanceof ReferenceType


                      At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.




                      That means you can do something like this:



                      Object o = new int 1,2 ;
                      System.out.println(o instanceof int); // prints "true"


                      You'd have to check if the object is an instanceof boolean, byte, short, char, int, long, float, double, or Object, if you want to detect all array types.



                      Also, an int is an instanceof Object, so depending on how you want to handle nested arrays, it can get complicated.



                      For the toString, java.util.Arrays has a toString(int) and other overloads you can use. It also has deepToString(Object) for nested arrays.



                      public String toString(Object arr) 
                      if (arr instanceof int)
                      return Arrays.toString((int) arr);
                      else //...



                      It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.



                      See also



                      • Managing highly repetitive code and documentation in Java

                      • Java Arrays.equals() returns false for two dimensional arrays.





                      share|improve this answer














                      You can use instanceof.



                      JLS 15.20.2 Type Comparison Operator instanceof




                       RelationalExpression:
                      RelationalExpression instanceof ReferenceType


                      At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.




                      That means you can do something like this:



                      Object o = new int 1,2 ;
                      System.out.println(o instanceof int); // prints "true"


                      You'd have to check if the object is an instanceof boolean, byte, short, char, int, long, float, double, or Object, if you want to detect all array types.



                      Also, an int is an instanceof Object, so depending on how you want to handle nested arrays, it can get complicated.



                      For the toString, java.util.Arrays has a toString(int) and other overloads you can use. It also has deepToString(Object) for nested arrays.



                      public String toString(Object arr) 
                      if (arr instanceof int)
                      return Arrays.toString((int) arr);
                      else //...



                      It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.



                      See also



                      • Managing highly repetitive code and documentation in Java

                      • Java Arrays.equals() returns false for two dimensional arrays.






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 23 '17 at 12:10









                      Community

                      11




                      11










                      answered Apr 27 '10 at 22:15









                      polygenelubricantspolygenelubricants

                      281k101503591




                      281k101503591











                      • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                        – edbras
                        Apr 28 '10 at 7:44






                      • 2




                        BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                        – edbras
                        Apr 28 '10 at 8:35










                      • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                        – polygenelubricants
                        Apr 28 '10 at 8:38










                      • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                        – edbras
                        Apr 28 '10 at 11:05










                      • @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                        – polygenelubricants
                        Apr 28 '10 at 11:16
















                      • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                        – edbras
                        Apr 28 '10 at 7:44






                      • 2




                        BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                        – edbras
                        Apr 28 '10 at 8:35










                      • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                        – polygenelubricants
                        Apr 28 '10 at 8:38










                      • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                        – edbras
                        Apr 28 '10 at 11:05










                      • @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                        – polygenelubricants
                        Apr 28 '10 at 11:16















                      Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                      – edbras
                      Apr 28 '10 at 7:44




                      Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T :(
                      – edbras
                      Apr 28 '10 at 7:44




                      2




                      2




                      BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                      – edbras
                      Apr 28 '10 at 8:35




                      BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()).
                      – edbras
                      Apr 28 '10 at 8:35












                      @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                      – polygenelubricants
                      Apr 28 '10 at 8:38




                      @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call.
                      – polygenelubricants
                      Apr 28 '10 at 8:38












                      It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                      – edbras
                      Apr 28 '10 at 11:05




                      It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int.class) Arrays.toString((int) array); Thanks all..
                      – edbras
                      Apr 28 '10 at 11:05












                      @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                      – polygenelubricants
                      Apr 28 '10 at 11:16




                      @edbras: That's how java.util.Arrays does it, yes. I see that you've been reading the code I linked to.
                      – polygenelubricants
                      Apr 28 '10 at 11:16











                      28














                      One can access each element of an array separately using the following code:



                      Object o=...;
                      if ( o.getClass().isArray() )
                      for(int i=0; i<Array.getLength(o); i++)
                      System.out.println(Array.get(o, i));




                      Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.






                      share|improve this answer


















                      • 2




                        isArray() was already adequately covered in the answers posted 4 years prior to this one.
                        – Jason C
                        Nov 17 '14 at 4:47






                      • 13




                        This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                        – Christopher Yang
                        Jan 13 '15 at 5:40











                      • @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                        – Stephen C
                        Nov 12 '18 at 23:59















                      28














                      One can access each element of an array separately using the following code:



                      Object o=...;
                      if ( o.getClass().isArray() )
                      for(int i=0; i<Array.getLength(o); i++)
                      System.out.println(Array.get(o, i));




                      Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.






                      share|improve this answer


















                      • 2




                        isArray() was already adequately covered in the answers posted 4 years prior to this one.
                        – Jason C
                        Nov 17 '14 at 4:47






                      • 13




                        This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                        – Christopher Yang
                        Jan 13 '15 at 5:40











                      • @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                        – Stephen C
                        Nov 12 '18 at 23:59













                      28












                      28








                      28






                      One can access each element of an array separately using the following code:



                      Object o=...;
                      if ( o.getClass().isArray() )
                      for(int i=0; i<Array.getLength(o); i++)
                      System.out.println(Array.get(o, i));




                      Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.






                      share|improve this answer














                      One can access each element of an array separately using the following code:



                      Object o=...;
                      if ( o.getClass().isArray() )
                      for(int i=0; i<Array.getLength(o); i++)
                      System.out.println(Array.get(o, i));




                      Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 19 '15 at 8:04









                      Dariusz

                      15.4k54379




                      15.4k54379










                      answered May 9 '14 at 15:25









                      user1928596user1928596

                      834916




                      834916







                      • 2




                        isArray() was already adequately covered in the answers posted 4 years prior to this one.
                        – Jason C
                        Nov 17 '14 at 4:47






                      • 13




                        This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                        – Christopher Yang
                        Jan 13 '15 at 5:40











                      • @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                        – Stephen C
                        Nov 12 '18 at 23:59












                      • 2




                        isArray() was already adequately covered in the answers posted 4 years prior to this one.
                        – Jason C
                        Nov 17 '14 at 4:47






                      • 13




                        This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                        – Christopher Yang
                        Jan 13 '15 at 5:40











                      • @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                        – Stephen C
                        Nov 12 '18 at 23:59







                      2




                      2




                      isArray() was already adequately covered in the answers posted 4 years prior to this one.
                      – Jason C
                      Nov 17 '14 at 4:47




                      isArray() was already adequately covered in the answers posted 4 years prior to this one.
                      – Jason C
                      Nov 17 '14 at 4:47




                      13




                      13




                      This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                      – Christopher Yang
                      Jan 13 '15 at 5:40





                      This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before.
                      – Christopher Yang
                      Jan 13 '15 at 5:40













                      @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                      – Stephen C
                      Nov 12 '18 at 23:59




                      @MaartenBodewes - I would use this link to decide what "not using reflection" means for GWT.
                      – Stephen C
                      Nov 12 '18 at 23:59











                      9














                      There is no subtyping relationship between arrays of primitive type, or between an array of a primitive type and array of a reference type. See JLS 4.10.3.



                      Therefore, the following is incorrect as a test to see if obj is an array of any kind:



                      // INCORRECT!
                      public boolean isArray(final Object obj)
                      return obj instanceof Object;



                      Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)




                      I use Google GWT so I am not allowed to use reflection :(




                      The best solution (to the isArray array part of the question) depends on what counts as "using reflection".



                      • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.



                      • Otherwise, the best way of figuring out whether an object has an array type is to use a sequence of instanceof expressions.



                        public boolean isArray(final Object obj) 
                        obj instanceof byte



                      • You could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.



                        public boolean isArray(final Object obj) 
                        return obj.getClass().toString().charAt(0) == '[';




                      1 - More precisely, the Class.isArray method is listed as supported by GWT in this page.






                      share|improve this answer



























                        9














                        There is no subtyping relationship between arrays of primitive type, or between an array of a primitive type and array of a reference type. See JLS 4.10.3.



                        Therefore, the following is incorrect as a test to see if obj is an array of any kind:



                        // INCORRECT!
                        public boolean isArray(final Object obj)
                        return obj instanceof Object;



                        Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)




                        I use Google GWT so I am not allowed to use reflection :(




                        The best solution (to the isArray array part of the question) depends on what counts as "using reflection".



                        • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.



                        • Otherwise, the best way of figuring out whether an object has an array type is to use a sequence of instanceof expressions.



                          public boolean isArray(final Object obj) 
                          obj instanceof byte



                        • You could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.



                          public boolean isArray(final Object obj) 
                          return obj.getClass().toString().charAt(0) == '[';




                        1 - More precisely, the Class.isArray method is listed as supported by GWT in this page.






                        share|improve this answer

























                          9












                          9








                          9






                          There is no subtyping relationship between arrays of primitive type, or between an array of a primitive type and array of a reference type. See JLS 4.10.3.



                          Therefore, the following is incorrect as a test to see if obj is an array of any kind:



                          // INCORRECT!
                          public boolean isArray(final Object obj)
                          return obj instanceof Object;



                          Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)




                          I use Google GWT so I am not allowed to use reflection :(




                          The best solution (to the isArray array part of the question) depends on what counts as "using reflection".



                          • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.



                          • Otherwise, the best way of figuring out whether an object has an array type is to use a sequence of instanceof expressions.



                            public boolean isArray(final Object obj) 
                            obj instanceof byte



                          • You could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.



                            public boolean isArray(final Object obj) 
                            return obj.getClass().toString().charAt(0) == '[';




                          1 - More precisely, the Class.isArray method is listed as supported by GWT in this page.






                          share|improve this answer














                          There is no subtyping relationship between arrays of primitive type, or between an array of a primitive type and array of a reference type. See JLS 4.10.3.



                          Therefore, the following is incorrect as a test to see if obj is an array of any kind:



                          // INCORRECT!
                          public boolean isArray(final Object obj)
                          return obj instanceof Object;



                          Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)




                          I use Google GWT so I am not allowed to use reflection :(




                          The best solution (to the isArray array part of the question) depends on what counts as "using reflection".



                          • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.



                          • Otherwise, the best way of figuring out whether an object has an array type is to use a sequence of instanceof expressions.



                            public boolean isArray(final Object obj) 
                            obj instanceof byte



                          • You could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.



                            public boolean isArray(final Object obj) 
                            return obj.getClass().toString().charAt(0) == '[';




                          1 - More precisely, the Class.isArray method is listed as supported by GWT in this page.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 12 '18 at 23:57

























                          answered Apr 27 '10 at 22:46









                          Stephen CStephen C

                          514k69563919




                          514k69563919



























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f2725533%2fhow-to-see-if-an-object-is-an-array-without-using-reflection%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







                              這個網誌中的熱門文章

                              What does pagestruct do in Eviews?

                              Dutch intervention in Lombok and Karangasem

                              Channel Islands