• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Used of toString

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Armel Moukoss
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Just to thank you very much guys for yours answers.

Armel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic