Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString() of a char array

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic