Java primitive types: boolean, byte, char, short, int, long, float, and double.
Java primitive wrappers: Boolean, Byte, Character, Short, Integer, Long, Float, and Double.
The pairs follow auto-boxing principle.
Wrapper classes offer certain amount of static methods, for instance, converting number in string into numeric values.
/*
* Class wrappers of primitive types offer static service methods.
* converting digit numbers from String to numeric primitive type.
* String class offers another way around, converting number to string.
*/
package NumberWraper;
/**
*
* @author YNZ
*/
public class UserNumbers {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double d = Double.parseDouble("20.456");
double a = Double.parseDouble("10.46");
double sum = a + d;
System.out.printf("%f + %f = %f \n ", d, a, sum);
//converting from numeric to string.
String aStr = String.valueOf(d);
String dStr = String.valueOf(a);
//valueOf srevice returns wrapper type
Double e = Double.valueOf(dStr);
}
}
You need watch out the number type even in the String format.
double ee = Double.parseDouble("20"); //1
int ff = Integer.parseInt("121.3"); //2
//1 is fine, for double may hold a int type without losing.
//2 is not valid for "121.3" is not integer.
No comments:
Post a Comment