1
String s="StackOverflow is heaven";
char []c=new Char[100];

c=s.toCharArray();

System.out.println(c);
System.out.println("Output="+c);

Output is :-  StackOverflow is heaven
              Output=[C@15db9742  

Why there is a difference B/w outputs? Please Explain it

Manish Sharma
  • 31
  • 1
  • 6
  • You are getting the address printed in the second print. But i dint know why. IS you first print correct? Is it not println(s)? – Superluminal Jul 15 '17 at 10:50
  • When you print an array, It will print the address of the array. Not the content in it. If you want that char[] to be a string use 'new string(c)'. – Kajal Jul 15 '17 at 10:51
  • 3
    I don't think this question is a duplicate. OP probably knows how to print an array, he just wonders why the reference is printed in the second case. – Alberto Trindade Tavares Jul 15 '17 at 10:51
  • It's printed because an array is essentially a subclass of `Object`, which doesn't override the default `#toString` (so it prints the `#hashcode` as well as the class type, in this case a `char`-primitive array aka `[C`). – Rogue Jul 15 '17 at 10:54
  • Replace the lines and then check what u get. – Pavan Kumar Jul 15 '17 at 10:55
  • @Rogue It doesn't work with `int[]`, so I'm not sure your explanation be correct. Apologies to everyone for marking duplicate so quickly; this is a good question, one of the few good ones all afternoon. – Tim Biegeleisen Jul 15 '17 at 10:57
  • @TimBiegeleisen An `int[]` prints `[I@hashcode`? Try it yourself: `System.out.println(new int[] {1, 2, 3});` – Rogue Jul 15 '17 at 11:04
  • @TimBiegeleisen May be you are trying to do `c = s.toCharArray();` ?? after changing it to `int[]`. – Suresh Atta Jul 15 '17 at 11:05
  • @SureshAtta [You get gibberish with an int array](http://rextester.com/DZH6426). – Tim Biegeleisen Jul 15 '17 at 11:06
  • @TimBiegeleisen I don't see any gibberish there. It is just your Integer arrays hex code. Just like char array. – Suresh Atta Jul 15 '17 at 11:18
  • Which lines I have to replace ?? @pawan sharma – Manish Sharma Jul 15 '17 at 11:34
  • Just interchange / shuffle the lines – Pavan Kumar Jul 16 '17 at 04:02

1 Answers1

5

Case1 :

The overloaded println which takes char[] method processed your array internally and printed the String. Note that println() has no such overloaded method for any other type of primitive array.

Case2 :

You are concatenating the array with String , hence before even processing by println, step 1 itself that your char arrays toString() gets called and appended to string and the final output printed as a String.

So if you expand your second statement, the processing looks like

System.out.println("Output="+c.toString());  
System.out.println("Output="+ "[C@15db9742");
System.out.println("Output=[C@15db9742");
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • When toString() method is called it append with string but final answer printed is String with the address of char Array[] Which I am not getting why? – Manish Sharma Jul 15 '17 at 11:38
  • 1
    @ManishSharma The "address-of..." mumbo-jumbo is just what `toString()` does for `char` arrays :-(. It's probably inheriting the default behavior from `Object`... – Kevin Anderson Jul 15 '17 at 12:07