File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes toString with Character Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "toString with Character Array" Watch "toString with Character Array" New topic

toString with Character Array

Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
I am not able to get, as to why the below program is printing JAVA

and why this one is printing the address

[ November 20, 2008: Message edited by: Martijn Verburg ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

You need to know two things to understand this:

(1) If you call toString() on an array, you always get a String that looks like "[C@24c21495". This is not the address of the array, but rather an indication of the type (the "[C" part), an @ sign, and the return value of calling hashCode() on the array.

(2) There are many different println() methods in the PrintStream class; the compiler chooses which one to call based on the type of the argument. There's a special one for a char[] arguments which prints each character from the array; i.e., it's as if you called

System.out.println(new String(charArray));

[Jess in Action][AskingGoodQuestions]
Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
Hey.! Got it..

wood burning stoves
subject: toString with Character Array
It's not a secret anymore!