Java Reference
In-Depth Information
and returns 1 , 0 , or -1 , if this number is greater than, equal to, or less than the other number.
For example,
new Double(12.4).compareTo(new Double(12.3)) returns 1 ;
new Double(12.3).compareTo(new Double(12.3)) returns 0 ;
new Double(12.3).compareTo(new Double(12.51)) returns -1 ;
The numeric wrapper classes have a useful static method, valueOf (String s) . This
method creates a new object initialized to the value represented by the specified string. For
example,
static valueOf methods
Double doubleObject = Double.valueOf( "12.4" );
Integer integerObject = Integer.valueOf( "12" );
You have used the parseInt method in the Integer class to parse a numeric string into
an int value and the parseDouble method in the Double class to parse a numeric string into
a double value. Each numeric wrapper class has two overloaded parsing methods to parse a
numeric string into an appropriate numeric value based on 10 (decimal) or any specified radix
(e.g., 2 for binary, 8 for octal, and 16 for hexadecimal).
static parsing methods
// These two methods are in the Byte class
public static byte parseByte(String s)
public static byte parseByte(String s, int radix)
// These two methods are in the Short class
public static short parseShort(String s)
public static short parseShort(String s, int radix)
// These two methods are in the Integer class
public static int parseInt(String s)
public static int parseInt(String s, int radix)
// These two methods are in the Long class
public static long parseLong(String s)
public static long parseLong(String s, int radix)
// These two methods are in the Float class
public static float parseFloat(String s)
public static float parseFloat(String s, int radix)
// These two methods are in the Double class
public static double parseDouble(String s)
public static double parseDouble(String s, int radix)
For example,
Integer.parseInt("11", 2) returns 3 ;
Integer.parseInt("12", 8) returns 10 ;
Integer.parseInt("13", 10) returns 13 ;
Integer.parseInt("1A", 16) returns 26 ;
Integer.parseInt("12", 2) would raise a runtime exception because 12 is not a
binary number.
Note that you can convert a decimal number into a hex number using the format method.
For example,
converting decimal to hex
String.format("%x", 26) returns 1A ;
 
 
Search WWH ::




Custom Search