Printing int array /string array
Find out the output for the following program:
public class Test3
{
public static void main(String[] args)
{
int[] array = {10,20,30};
System.out.println(array);
}
}
- 10 20 30
- 10
- Some random value representing the address of the handle array
- Runtime error
Notes:
Anytime you use "System.out.println()" and pass in an object reference, java will call that underlying object's toString() method. It doesn't matter if it's an array, a String, an Integer, a Dog, a Fubar or what.
In your example, you are passing in an object that is an array (doesn't matter what the specific type is). Since the Arrays class does not override the Object class' toString() method, you get the default behavior of the Object class' toString() method.
You would see something VERY similar if you created your own Fubar class and did a System.out.println on it.
However, if in your class definition, you override the toString() method with something meaningful, when you sent it to S.o.p(), you will get your newly defined toString() behavior.