File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes overriding toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding toString()" Watch "overriding toString()" New topic

overriding toString()

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
class arr1
public static void main(String a[])
int[] b = ( 1, 2, 3};
System.out.println("string " + b);
/* According to JLS, string concat. operator + and a reference (in this case array b) will convert the reference to a String by invoking toString method of the referenced object. */
Now I want to override toString() of Object.
I do not know where to include this code:
public String toString()
return "array string" ;
So when I run above code , instead of generating
string : some junk
i need
string : array string // overrides
Jane Rozen
Ranch Hand

Joined: Feb 12, 2000
Posts: 53

is this what you want to achieve?
I agree. Here's the link:
subject: overriding toString()
It's not a secret anymore!