*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() of a char array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() of a char array" Watch "toString() of a char array" New topic
Author

toString() of a char array

Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Is the toSting() method of a char[] overridden? For an array of any type, if we print out the array name, we get the actual object address, but for char array, we get a neat listing of the actual contects.


The output is of the type:
[I@3e25a5
[F@19821f
[Ljava.lang.String;@addbf1

dnfn


The last one is the actual contents.

Anyone know why it is so?

Thanks!


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

I'm not sure about the question's answer.
But for you info by
System.out.println(ch);

you are not calling char[]`s toString() method.

SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
because thats how sequence of characters is handled by java. System.out.println doesn't call the "toString()" method on char arrays.
If you explicitly do ch.toString(), then the address of array will be printed just like other arrays.


SCJP 1.6 96%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

for details take a look into implementation of following methods:
println(char[]) -- print(char[]) -- write(int)
@
http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#println(char[])
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

[quote
]for details take a look into implementation of following methods:
println(char[]) -- print(char[]) -- write(int)


Thanks Minhaj, Neha,

My mistake, toString() is not called by println(). The method is overloaded for all of those. Interestingly, for the arrays, it is overloaded only for char[]. I guess rest of the arrays just go into the overloaded version that accepts an Object as argument.
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString() of a char array
 
Similar Threads
Converting Char array to String
Dsplaying the alphabet with a loop?
NPE from K&B example SCJP6
analyze shape
Basic Java Problem....HELP!!!!