✏️ Explanatory Question

[Primitive Values, Wrapper Classes, Types and Casting]

Following is a code to convert a String into Double. Fill the missing parts of the code:
________ str = "15.45"; double d = __________ (Double.parseDouble(str));

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Correct Code:
String str = "15.45"; double d = Double.parseDouble(str);

Step 1: Identify First Blank

The variable str stores text:

"15.45"

Therefore, its datatype must be:

String

Step 2: Identify Second Blank

The method:

Double.parseDouble(str)

already converts the string into a double value.

So no extra method or wrapping is required.

Final Code:

String str = "15.45"; double d = Double.parseDouble(str);

How It Works:

Component Purpose
String str Stores numeric value in text format
Double.parseDouble() Converts String → double primitive

Important Concept:

Wrapper Class → Double Conversion Method → parseDouble()

Other Similar Methods:

Method Conversion
Integer.parseInt() String → int
Double.parseDouble() String → double
Float.parseFloat() String → float

Conclusion:

  • First blank → String
  • Second blank → Double.parseDouble(str)
  • Result → String converted to double