请告诉我这两种方法的区别。提前谢谢。
答案 0 :(得分:9)
Java toString()方法:
如果要将任何对象表示为字符串,请使用toString()方法 beString()方法返回字符串 对象的表示。
示例:
Student s1 = new Student(101,"Raj","lucknow");
Student s2 = new Student(102,"Vijay","ghaziabad");
System.out.println(s1);//compiler writes here s1.toString()
System.out.println(s2);//compiler writes here s2.toString()
//Output : 101 Raj lucknow
102 Vijay ghaziabad
Java toPlainString()方法:
java.math.BigDecimal.toPlainString()返回一个字符串 没有指数字段的BigDecimal的表示。
示例:
MathContext mc = new MathContext(3); // 3 precision
BigDecimal bigDecimal = new BigDecimal("1234E+4", mc);
// Assign the plain string value of bigDecimal to s
String plainString = bigDecimal.toPlainString();
String str = "Plain string value of " + bigDecimal + " is " + plainString;
// print s value
System.out.println( str );
//Output : Plain string value of 1.23E+7 is 12300000