Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different behaviour of println() in java

Tags:

java

println

//take the input from user
text = br.readLine();

//convert to char array
char ary[] = text.toCharArray();


System.out.println("initial string is:" + text.toCharArray());

System.out.println(text.toCharArray());

Output:

initial string is:[C@5603f377
abcd
like image 678
dev Avatar asked Nov 25 '13 17:11

dev


1 Answers

println() is overloaded to print an array of characters as a string, which is why the 2nd print statement works correctly:

public void println(char[] x)

Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

Parameters:
x - an array of chars to print.

The 1st println() statement, on the other hand, concatenates the array's toString() with another string. Since arrays don't override toString(), they default to Object's implementation, which is what you see.

like image 75
arshajii Avatar answered Sep 20 '22 03:09

arshajii