5

I have

char c1 = 'S';           // S as a character
char c2 = '\u0068';      // h in Unicode
char c3 = 0x0065;        // e in hexadecimal
char c4 = 0154;          // l in octal
char c5 = (char) 131170; // b, casted (131170-131072=121)
char c6 = (char) 131193; // y, casted (131193-131072=121)
char c7 = '\'';          // ' apostrophe special character
char c8 = 's';           // s as a character
char[] autoDesignerArray = {c1, c2, c3, c4, c5, c6, c7, c8};

And

System.out.println(autoDesignerArray + "Mustang"); 

Output: [C@c17164Mustang

System.out.println(autoDesignerArray);

Output: Shelby's

I'm not understanding why I get the weird output when I concatenate the char array with a string. What is the "[C@c17164"? The location in memory? And why do I get that when I concatenate with a string, but I get what I would expect when I print it alone?

Mike P
  • 419
  • 1
  • 7
  • 16
  • `System.out.println(autoDesignerArray + "Mustang");` and `System.out.println(autoDesignerArray);` are not the same method. `System.out.println` provide an overload that handle `char` array – ortis Apr 02 '15 at 14:16

4 Answers4

7

The expression System.out.println(X + Y) is equal to the expression System.out.println(X.toString() + Y.toString()).

When you call System.out.println(autoDesignerArray + "Mustang") autoDesignerArray.toString() (which is "[C@c17164") is concatenated with "Mustang" and the result is printed.

Dandelion
  • 744
  • 2
  • 13
  • 34
vahidreza
  • 843
  • 1
  • 8
  • 19
  • According to your 2nd point When you call System.out.println(autoDesignerArray) autoDesignerArray.toString() (which is "[C@c17164") should be printed but it prints Shelby's. Without String concatenation with array prints actual data but if we concatenate array with String then it prints hashcode address. – Vikas Jul 21 '16 at 21:02
  • @VikasKumarSingh "*According to your 2nd point When you call System.out.println(autoDesignerArray) autoDesignerArray.toString() (which is "[C@c17164") should be printed*" not quite, `autoDesignerArray.toString()` returns string `"[C@c17164"`, but `println(autoDesignerArray)` doesn't involve `toString()`, it simply prints each character from array. It is possible because in `PrintStream` (which `System.out` is instance of) exist few overloaded `println` methods, and one of them is `println(char[])`. – Pshemo Jul 21 '16 at 21:40
2

Since every has array has a class the string you get is the object representation of its object i.e. [C@c17164Mustang where

  • [C is a class name ([ represent 1d array)
  • @ concates the the string
  • c17164 some hash code
  • Mustang your string

to check the class name of array do System.out.println(yourArray.getClass().getName());

For ex if you do System.out.println(new Object()); you will get something like java.lang.Object@25154f the string representation of object created.

And to print the actual values of array do System.out.println((java.util.Arrays.toString(autoDesignerArray))); which gives

[S, h, e, l, b, y, ', s]

Demo

singhakash
  • 7,891
  • 6
  • 31
  • 65
0

Arrays in java don't have overwritten toString() method, which means that:

  1. System.out.println(autoDesignerArray + "Mustang");
    • will print default toString() of array and concatenate it with the string
    • the default implementation of toString() will print the binary name followed by hashCode() (char array will print [C followed by it's hash code)
  2. System.out.println(autoDesignerArray);
    • will actually call Arrays.toString() or similar functionality handling the toString() of arrays correctly
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • 1
    I'm pretty sure that the 2nd point is due to [this](https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-char:A-). Also not sure about the first one. – npinti Apr 02 '15 at 14:15
0

This has to do with Arrays and why the fact that they don't implicitly inherit Object - For more information feel free to check out this SO question (have an answer there).

println(char[] s) does somewhat confuse me in the Oracle Doc - In C you would generally iterate through every element within the array and print each element follow by a \n to break the line.

However the jist of it all is that autoDesignerArray.toString() won't really work as you wish it would (Which is why it returns [C@c17164).

Community
  • 1
  • 1
Juxhin
  • 5,068
  • 8
  • 29
  • 55