请告知我这两种方法之间的区别。谢谢提前。
请告知我这两种方法之间的区别。谢谢提前。
Java toString()方法:
如果您想将任何对象表示为字符串,则使用toString()方法。 toString()方法返回对象的字符串表示形式。
示例:
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