Java xxxValue() Method: Usage and Examples
Table of Content:
Numeric wrapper classes are very similar to each other. Each contains the methods
doubleValue()
, floatValue()
, intValue()
, longValue()
,
shortValue()
, andbyteValue()
.
These methods convert objects into primitive type values.
The wrapper classes do not have no-arg constructors. The instances of all wrapper classes are immutable; this means that, once the objects are created, their internal values cannot be changed.
The abstract class Number defines a superclass that is implemented by the classes that wrap
the numeric types byte, short, int, long, float, and double
. Number has abstract methods
that return the value of the object in each of the different number formats. For example,
doubleValue( ) returns the value as a double
, floatValue( ) returns the value
as a float
, and
so on.
xxxValue () method
The method converts the value of the Number Object that invokes the method to the primitive data type that is returned from the method. The wrapper classes provide constructors, constants, and conversion methods for manipulating various data types.
xxxValue () method is used to convert an object to the valuexxx Number data type and return.
Here is a separate method for each primitive data type ?
byte byteValue() short shortValue() int intValue() long longValue() float floatValue() double doubleValue()
Related methods and it's Description:
Types of | Method and Description |
---|---|
byte |
byteValue (): Returns a byte value. |
abstract double |
doubleValue (): In double Returns the specified value. |
abstract float |
floatValue (): Returns a float value. |
abstract int |
intValue (): Returns as int the specified value. |
abstract long |
longValue (): In long form returns the specified value. |
short |
shortValue (): In short form returns the specified value. |
Parameters
Here is the detail of parameters ?
- All these are default methods and accepts no parameter. The wrapper classes do not have no-arg constructors
Return Value
-
These methods convert objects into primitive type values. This method returns the primitive data type that is given in the signature. The values returned by these methods can be rounded
Number has six concrete subclasses that hold explicit values of each numeric type: Double, Float, Byte, Short, Integer, and Long.
Most wrapper class names for a primitive type are the same as the primitive data type
name with the first letter capitalized. The exceptions are Integer
and
Character
Example:
Program:
public class ValueMethod { public static void main(String args[]) { Integer x = 12; // Returns byte primitive data type System.out.println( x.byteValue() ); // Returns double primitive data type System.out.println(x.doubleValue()); // Returns long primitive data type System.out.println( x.longValue() ); } }
Output:
12 12.0 12 Press any key to continue . . .
The wrapper classes provide constructors, constants, and conversion methods for manipulating various data types.
java.lang.Integer-value: int
+MAX_VALUE: int
+MIN_VALUE: int
+Integer(value: int)
+Integer(s: String)
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue(): double
+compareTo(o: Integer): int
+toString(): String
+valueOf(s: String): Integer
+valueOf(s: String, radix: int): Integer
+parseInt(s: String): int
+parseInt(s: String, radix: int): int
The wrapper classes provide constructors, constants, and conversion methods for manipulating various data types.
java.lang.Double-value: double
+MAX_VALUE: double
+MIN_VALUE: double
+Double(value: double)
+Double(s: String)
+byteValue(): byte
+shortValue(): short
+intValue(): int
+longVlaue(): long
+floatValue(): float
+doubleValue(): double
+compareTo(o: Double): int
+toString(): String
+valueOf(s: String): Double
+valueOf(s: String, radix: int): Double
+parseDouble(s: String): double
+parseDouble(s: String, radix: int): double
Each numeric wrapper class has the constants MAX_VALUE and MIN_VALUE . MAX_VALUE represents the maximum value of the corresponding primitive data type. For Byte , Short , Integer , and Long , MIN_VALUE represents the minimum byte , short , int , and long val- ues. For Float and Double , MIN_VALUE represents the minimum positive float and double values. The following statements display the maximum integer (2,147,483,647), the minimum positive float (1.4E–45), and the maximum double floating-point number (1.79769313486231570e+308d).
System.out.println("The maximum integer is " + Integer.MAX_VALUE); System.out.println("The minimum positive float is " +Float.MIN_VALUE); System.out.println("The maximum double-precision floating-point number is " +Double.MAX_VALUE);
Float and Double define the following constants:
Serial | wrapper the constants | Description |
1 | MAX_EXPONENT | Maximum exponent (Added by Java SE 6.) |
2 | MAX_VALUE | Maximum positive value |
3 | MIN_EXPONENT | Minimum exponent (Added by Java SE 6.) |
4 | MIN_NORMAL | Minimum positive normal value (Added by Java SE 6.) |
5 | MIN_VALUE | Minimum positive value |
6 | NaN | Not a number |
7 | POSITIVE_INFINITY | Positive infinity |
6 | NEGATIVE_INFINITY | Negative infinity |
9 | SIZE | The bit width of the wrapped value |
10 | TYPE | The Class object for float or double |
Number instance methods :
Method | Description |
---|---|
compareTo() |
Compares this Number object to the argument. |
equals() |
Determines whether this number object is equal to the argument. |
valueOf() |
Returns an Integer object holding the value of the specified primitive. |
toString() |
Returns a String object representing the value of specified int or Integer. |
parseInt() |
This method is used to get the primitive data type of a certain String. |
abs() |
Returns the absolute value of the argument. |
ceil() |
Returns the smallest integer that is greater than or equal to the argument. Returned as a double. |
floor() |
Returns the largest integer that is less than or equal to the argument. Returned as a double. |
rint() |
Returns the integer that is closest in value to the argument. Returned as a double. |
round() |
Returns the closest long or int, as indicated by the method's return type, to the argument. |
min() |
Returns the smaller of the two arguments. |
max() |
Returns the larger of the two arguments. |
exp() |
Returns the base of the natural logarithms, e, to the power of the argument. |
log() |
Returns the natural logarithm of the argument. |
pow() |
Returns the value of the first argument raised to the power of the second argument. |
sqrt() |
Returns the square root of the argument. |
sin() |
Returns the sine of the specified double value. |
cos() |
Returns the cosine of the specified double value. |
tan() |
Returns the tangent of the specified double value. |
asin() |
Returns the arcsine of the specified double value. |
acos() |
Returns the arccosine of the specified double value. |
atan() |
Returns the arctangent of the specified double value. |
atan2() |
Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta. |
toDegrees() |
Converts the argument to degrees. |
toRadians() |
Converts the argument to radians. |
toHexString() |
Returns a string holding the value of specified primitive. |
toBinaryString() |
Returns a string that contains the binary equivalent of specified primitive. |
toOctalString() |
Returns a string that contains the octal equivalent of specified primitive. |
bitCount() |
Returns the number of set bits of specified primitive. |
reverse() |
Reverse the order of the bits in specified primitive and returns the result. |
rotateLeft(long num, int n) |
Returns the result of rotating in specified primitive num left n positions. |
rotateRight(long num, int n) |
Returns the result of rotating in specified primitive num right n positions. |
signum() |
Returns -1 if specified primitive is negative, 0 if it is zero, and 1 if it is positive. |