Is the pattern in the int value 221 the same pattern as in the float value 221.0?
No. Integer and floating point types use bits in fundamentally different ways.
Here is a program that converts a String of characters into primitive type double. In this program, the characters that are converted are contained in a String literal. (We will do IO later on). The program will not run under versions of Java earlier than 1.2 because they do not have the parseDouble() method.
// This program requires Java 1.2 or higher
//
import java.io.*;
class StringToDouble
{
public static void main (String[] args)
{
final String charData = "3.14159265";
double value;
value = Double.parseDouble( charData ) ;
System.out.println("value: " + value +" twice value: " + 2*value );
}
}
|
The reserved word final means that the value inside the variable charData will not change. This program is recommended for "copy-paste-and-run." Running the program writes the following to the screen:
C:\temp>java StringToDouble value: 3.14159265 twice value: 6.2831853 |
The details of how this program works are on the next page.