Movatterモバイル変換


[0]ホーム

URL:


Programming & Design
Question:
java - toString() method?
anonymous
2010-05-07 03:11:45 UTC
in my coursework spec, it says "all classes should implement a toString() methond to make it simple to print out all information about each class". how do i go about doing this?
Three answers:
keira
2010-05-07 03:32:20 UTC
dude then make a toString () method



public String toString() {



return (****then the variables to be printed out****);



}
?
2010-05-07 03:50:30 UTC
Implementing toString method in java is done by overriding the Object’s toString method. The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object. Below is a program showing the use of the Object’s Default toString java method.



class PointCoordinates {



private int x, y;

public PointCoordinates(int x, int y) {

this.x = x;

this.y = y;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

}



public class ToStringDemo {



public static void main(String args[]) {

PointCoordinates point = new PointCoordinates(10, 10);

// using the Default Object.toString() Method

System.out.println("Object toString() method : " + point);

// implicitly call toString() on object as part of string concatenation

String s = point + " testing";

System.out.println(s);

}

}



In the above example when we try printing PointCoordinates object, it internally calls the Object’s toString() method as we have not overridden the java toString() method. Since out example has no toString method, the default one in Object is used. The format of the default toString method of the Object is as shown below.

Class Name, “@”, and the hex version of the object’s hashcode concatenated into a string.

The default hashCode method in Object is typically implemented by converting the memory address of the object into an integer.



Below is an example shown of the same program by Overriding the default Object toString() method. The toString() method must be descriptive and should generally cover all the contents of the object.



class PointCoordinates {



private int x, y;

public PointCoordinates(int x, int y) {

this.x = x;

this.y = y;

}

public int getX() {

return x;

}

public int getY() {

return y;

}

// Custom toString() Method.

public String toString() {

return "X=" + x + " " + "Y=" + y;

}

}



public class ToStringDemo2 {



public static void main(String args[]) {

PointCoordinates point = new PointCoordinates(10, 10);

System.out.println(point);

String s = point + " testing";

System.out.println(s);

}

}
anonymous
2016-02-28 04:19:49 UTC
ask an IT


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading onnarkive:
Search results for'java - toString() method?' (Questions and Answers)
4
replies
Java: Can you call a toStringmethod using a"get" accessormethod?
started2010-05-02 13:51:52 UTC
programming & design
3
replies
Java Moneymethod question?
started2013-02-07 00:25:53 UTC
programming & design
3
replies
problem with override tostringmethod injava?
started2013-07-28 06:06:46 UTC
programming & design
3
replies
JavatoString()method help?
started2012-03-13 16:24:20 UTC
programming & design
about -legalese
Loading...

[8]ページ先頭

©2009-2025 Movatter.jp