2015-11-06 355 views

回答

4

Java的toString()方法:

如果你想表示任何对象作为一个字符串,toString()方法 进入existence.The 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 

爪哇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 
+1

可能值得一提的是每个Object都有toString()方法,因为它在Object类本身中定义了! – ParkerHalo

+0

Double numDb = 1.00000E-28; BigDecimal b = new BigDecimal(numDb); String s = b.toString(); System.out.println(s); 上面的代码片段对我来说工作正常。这就是为什么我混淆了这两种方法之间的区别。 –

相关问题