How to see if an object is an array without using reflection?
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
add a comment |
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
add a comment |
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
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
java arrays gwt instanceof
edited Apr 29 '14 at 13:44
bluish
13.8k1693147
13.8k1693147
asked Apr 27 '10 at 22:10
edbrasedbras
1,39252652
1,39252652
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
You can use instanceof.
JLS 15.20.2 Type Comparison Operator instanceof
RelationalExpression:
RelationalExpression instanceof ReferenceType
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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.
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 howjava.util.Arraysdoes it, yes. I see that you've been reading the code I linked to.
– polygenelubricants
Apr 28 '10 at 11:16
|
show 1 more comment
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.
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
add a comment |
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
instanceofexpressions.public boolean isArray(final Object obj)
obj instanceof byteYou 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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Apr 27 '10 at 23:49
answered Apr 27 '10 at 23:26
Steve KuoSteve Kuo
31.9k69176235
31.9k69176235
add a comment |
add a comment |
You can use instanceof.
JLS 15.20.2 Type Comparison Operator instanceof
RelationalExpression:
RelationalExpression instanceof ReferenceType
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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.
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 howjava.util.Arraysdoes it, yes. I see that you've been reading the code I linked to.
– polygenelubricants
Apr 28 '10 at 11:16
|
show 1 more comment
You can use instanceof.
JLS 15.20.2 Type Comparison Operator instanceof
RelationalExpression:
RelationalExpression instanceof ReferenceType
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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.
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 howjava.util.Arraysdoes it, yes. I see that you've been reading the code I linked to.
– polygenelubricants
Apr 28 '10 at 11:16
|
show 1 more comment
You can use instanceof.
JLS 15.20.2 Type Comparison Operator instanceof
RelationalExpression:
RelationalExpression instanceof ReferenceType
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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.
You can use instanceof.
JLS 15.20.2 Type Comparison Operator instanceof
RelationalExpression:
RelationalExpression instanceof ReferenceType
At run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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.
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 howjava.util.Arraysdoes it, yes. I see that you've been reading the code I linked to.
– polygenelubricants
Apr 28 '10 at 11:16
|
show 1 more comment
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 howjava.util.Arraysdoes 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
|
show 1 more comment
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
instanceofexpressions.public boolean isArray(final Object obj)
obj instanceof byteYou 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.
add a comment |
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
instanceofexpressions.public boolean isArray(final Object obj)
obj instanceof byteYou 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.
add a comment |
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
instanceofexpressions.public boolean isArray(final Object obj)
obj instanceof byteYou 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.
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
instanceofexpressions.public boolean isArray(final Object obj)
obj instanceof byteYou 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.
edited Nov 12 '18 at 23:57
answered Apr 27 '10 at 22:46
Stephen CStephen C
514k69563919
514k69563919
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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