Different behavior of println () in java

//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
+6
source share
1 answer

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

public void println(char[] x)

Prints an array of characters and then completes the line. This method behaves as if it calls print(char[]) and then println() .

Options:
x is an array of characters to print. A.

The first println() statement, on the other hand, concatenates the toString() array with another string. Since arrays do not override toString() , they default to an Object implementation , which is what you see.

+12
source

Source: https://habr.com/ru/post/958789/


All Articles