Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Used of toString Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Used of toString" Watch "Used of toString" New topic
Author

Used of toString

Armel Moukoss
Greenhorn

Joined: Mar 28, 2007
Posts: 14
Hi,

I don't really understand how the toString method is used in the code below :

One of the possible result :


Initial State: [k, q, i, j, f, V, J, S, c, v, u', U, C, z, I, y, d, H, X, e]
Sorted: [C, H, I, J, S, U, V, X, c, d, e, f, i, j, k, q, u', v, y, z]
Reversed: [z, y, v, u', q, k, j, i, f, e, d, c, X, V, U, S, J, I, H, C]



I don't see any place where the toString method is called. But if i put it in commentary the result is not the same. Can someone help me, please ? thank you.

Ps : I take the code on the cd of the book "complete java 2 certification", in solution of the chapter 8.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

In this code example, I believe that the toString() method is called by the following code:



The first parameter to println() is a String, so the "+" sign means string concatenation (not addition) and so the vec variable will be converted to a String, using its toString() method, since it is in a String context. The Vector must, in turn, call toString() for each element in the Vector.

Kaydell
[ May 28, 2007: Message edited by: Kaydell Leavitt ]
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
Its called when System.out.println() is invoked. Even you can override to print in the format you want.




Regards
Nik
Scjp 1.5
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Heres a simple example



Try doing this

System.out.println(new Test());

You should see the custom definition of the class on your console instead of its memory location.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Armel Moukoss
Greenhorn

Joined: Mar 28, 2007
Posts: 14
Hi,

Just to thank you very much guys for yours answers.

Armel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Used of toString
 
Similar Threads
'a href' in firefox
June Newsletter Puzzle
Dsplaying the alphabet with a loop?
java problem
HELP~~