Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding toString()

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is this what you want to achieve?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic