A colleague came across this behaviour while using ternary operator inside the Arrays.asList(T...)
public static void main(String[] args) throws Exception {
System.out.println(Arrays.asList(new String[]{"2"}));
System.out.println(Arrays.asList(false? new int []{2} :new String[]{"2"}));
}
The first sout prints: [2]
The second one prints: [[Ljava.lang.String;@36baf30c]
The java version used is java 8.
Shouldn't the second sout print the same result as the first?
Arrays.asList
has varargs parameter, and it can be called in two ways:
The first version of your code
Arrays.asList(new String[]{"2"}))
is interpreted as you passing a String array in place of the varargs argument to asList
, so you get a String list containing the contents of the array, which is the element "2".
In the second version, the type of the expression
(false? new int []{2} :new String[]{"2"})
is Object, since int[]
and String[]
have no other common base type, and an int array cannot be interpreted as an Object array.
So your asList
call resolves to a varargs call with one Object argument.
That gives you a list containing one element, where that element itself is your array.
The parameter of Arrays.asList is a varargs parameter, T...
, which is syntactic sugar that lets you call it in either of two ways:
T[]
, in which case the result of that expression is passed directly into Arrays.asList.
String[]
, so T
is String
and the array is passed into Arrays.asList.T
, in which case the results of those expressions get bundled up into an array which gets passed into Arrays.asList.
Object
, so T
is Object
and a single-element array containing that argument gets passed into Arrays.asList.This is because int
isn't a subtype of Object
, so int[]
isn't a subtype of Object[]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With