You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于Double.parseDouble与Double.valueOf的用法、定义及差异的技术咨询

Hey there! Let's tackle your questions about Java's Double class methods clearly and practically.

What is Double.parseDouble(String)?

Double.parseDouble(String s) is a static method in the java.lang.Double class built specifically to convert a string representation of a number into a primitive double value. It’ll throw a NumberFormatException if the input string isn’t a valid double format (like if it contains non-numeric characters that don’t fit, e.g., "3.14a" or "hello").

How to convert String to double using Double.parseDouble(String) and Double.valueOf(String)?

Quick note first: I think you might have meant Double.valueOf(String) instead of Double.valueOf(double)—since Double.valueOf(double) takes a primitive double and returns a Double wrapper object, which doesn’t directly help with String-to-double conversion. Let’s cover the correct String-based method and clarify the double-input variant too.

Using Double.parseDouble(String)

This is the most direct way to get a primitive double from a string:

public class StringToDoubleDemo {
    public static void main(String[] args) {
        String priceStr = "29.99";
        try {
            double price = Double.parseDouble(priceStr);
            System.out.println("Converted value: " + price); // Outputs 29.99
        } catch (NumberFormatException e) {
            System.err.println("Oops, invalid number format: " + priceStr);
        }
    }
}

Always wrap this in a try-catch block if your input might be invalid—it prevents runtime crashes from bad data.

Using Double.valueOf(String)

This method returns a Double wrapper object instead of a primitive, but Java’s autoboxing/unboxing lets you easily get a primitive double from it:

public class StringToDoubleDemo {
    public static void main(String[] args) {
        String tempStr = "-15.3";
        try {
            // Option 1: Get the wrapper object first, then unbox to primitive
            Double tempObj = Double.valueOf(tempStr);
            double tempPrimitive = tempObj;
            
            // Option 2: Directly unbox in one line
            double tempPrimitive2 = Double.valueOf(tempStr);
            
            System.out.println("Converted value: " + tempPrimitive); // Outputs -15.3
        } catch (NumberFormatException e) {
            System.err.println("Invalid number format: " + tempStr);
        }
    }
}

If you did mean Double.valueOf(double), that’s just for wrapping a primitive double into a Double object—so you’d first use parseDouble to get the primitive, then pass it to valueOf:

double piPrimitive = Double.parseDouble("3.14159");
Double piWrapper = Double.valueOf(piPrimitive);
Key Differences Between Double.parseDouble(String) and Double.valueOf(String)

Let’s break down the core distinctions:

  • Return Type:
    • parseDouble returns a primitive double directly.
    • valueOf returns a Double wrapper class object.
  • Caching Behavior:
    • Double.valueOf(String) internally calls parseDouble to get the primitive, then uses Double.valueOf(double) which caches Double objects for integer values between -128 and 127. This means repeated conversions of strings like "42" or "-100" reuse the same object, saving memory.
    • parseDouble doesn’t involve any caching—it always returns a new primitive value (though primitives have no object overhead anyway).
  • Use Cases:
    • Use parseDouble when you explicitly need a primitive double (e.g., for calculations, primitive variable assignments).
    • Use valueOf when you need a Double object (e.g., storing in collections like ArrayList<Double>, working with generic types) or when leveraging caching for frequent small integer values.
  • Performance:
    • For small integer values in the cached range, valueOf is more efficient for repeated conversions since it reuses objects.
    • For non-cached values or one-off conversions, the performance difference is negligible—choose based on your return type needs.

内容的提问来源于stack exchange,提问作者Yohan Malshika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:39:28