Movatterモバイル変換


[0]ホーム

URL:


Programming & Design
Question:
Java toString() method help?
OwenMM
2012-03-13 16:24:20 UTC
Two questions:

What is the toString method and why would you use it?

Can you please provide me an example?

Thanks
Three answers:
God Z
2012-03-13 16:57:17 UTC
the toString() method is a representative of your object. Let me show you an example:



public class NewTo

{

//varaibles

double x;

double y;



//contructor

public NewTo(double x, double y)

{

this.x = x;

this.y = y;

}



//methods

public String toString()

{

return x + "/" + y;

}





//Main method



public static void main(String[] args)

{

NewTo Java = new NewTo(5,2);

System.out.println(Java);

}

}
anonymous
2016-05-17 13:01:55 UTC
It really depends on the object's purpose as to the usefulness of a toString() method. If the object is data related, the toString() method would be a simple way to output the data as text, no matter what the input type is. This would be useful for user feedback or debugging/logging. For non-data type objects (i.e. a stream, database connection, recordset, etc...), a text dump could be useful for debugging.
?
2012-03-13 16:25:10 UTC
ask an IT


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
about -legalese
Loading...

[8]ページ先頭

©2009-2025 Movatter.jp