Given the variables:
Object[] ab = new Object[] { "a", "b" };
Object[] cd = new Object[] { "c", "d" };
When calling the following method:
public static void m(Object... objects) {
System.out.println(Arrays.asList(objects));
}
Using:
m(ab, cd);
I get the expected output:
[[Ljava.lang.Object;@3e25a5, [Ljava.lang.Object;@19821f]
But when using:
m(ab);
I get:
[a, b]
Since strings <- ab
and not strings[0] <- ab
.
How can I force the compiler to take the ab
array as the first value of the strings
array, and then having the output:
[Ljava.lang.Object;@3e25a5
?
Typecast it while passing and you will get what you want -
m((Object)ab);
Apart from as suggested by @Sudhansu. You can define the variables as below so you don't have to bother with casting in method call when passing single array.
Object ab = new Object[] { "a", "b" };
Object cd = new Object[] { "c", "d" };
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