String str ="123"; double ty=Double.valueOf(str); System.out.println(ty); double ty1=Double.max(1, 2); double ty2=Double.parseDouble(str); System.out.println(ty1); System.out.println(ty2);
输出:
123.0 2.0 123.0
方法valuOf会new对象,转换对象更繁琐。返回Double对象
public static Double valueOf(String s) throws NumberFormatException { return new Double(parseDouble(s)); }
方法parseDouble,返回的double对象
public static double parseDouble(String s) throws NumberFormatException { return FloatingDecimal.parseDouble(s); }
补充:
double是基本数据类型,Double是原始数据类型
double没有方法,Double有自己的属性和方法
double只创建引用,Double创建对象
集合类不能存放double,只能存放Double
double存放在栈中,Double存放在堆中
栈的存取速度要高于堆,另外栈中的数据可以共享