Thursday 16 March 2017

Simple Type Wrappers - Java Tutorials ( Page - 1 of 2 )

As we mentioned in Part I of this book, Java uses simple types, such as int and char, for performance reasons. These data types are not part of the object hierarchy. They are passed by value to methods and cannot be directly passed by reference. Also, there is no way for two methods to refer to the same instance of an int. At times, you will need to create an object representation for one of these simple types. For example, there are enumeration classes discussed in Chapter 15 that deal only with objects; to store a simple type in one of these classes, you need to wrap the simple type in a class. To address this need, Java provides classes that correspond to each of the simple types. In essence, these classes encapsulate, or wrap, the simple types within a class. Thus, they are commonly referred to as type wrappers.


Number

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. That is, doubleValue( ) returns the value as a double, floatValue( ) returns the value as a float, and so on. These methods are shown here:

      byte byteValue( )
      double doubleValue( )
      float floatValue( )
      int intValue( )
      long longValue( )
      short shortValue( )

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.


Double and Float

Double and Float are wrappers for floating-point values of type double and float, respectively. The constructors for Float are shown here:

      Float(double num)
      Float(float num)
      Float(String str) throws NumberFormatException

As you can see, Float objects can be constructed with values of type float or double. They can also be constructed from the string representation of a floating-point number. The constructors for Double are shown here:

      Double(double num)
      Double(String str) throws NumberFormatException

Double objects can be constructed with a double value or a string containing a floating-point value.

The methods defined by Float are shown in Table 14-1. The methods defined by Double are shown in Table 14-2. Both Float and Double define the following constants:

MAX_VALUE                          Maximum positive value
MIN_VALUE                            Minimum positive value
NaN                                           Not a number
POSITIVE_INFINITY              Positive infinity
NEGATIVE_INFINITY            Negative infinity
TYPE                                         The Class object for float or double


The Methods Defined by Float

byte byteValue( ):   Returns the value of the invoking object as a byte.

static int compare(float num1, float num2):  Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2. (Added by Java 2, version 1.4)

int compareTo(Float f):   Compares the numerical value of the invoking object with that of  f. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by Java 2)

int compareTo(Object obj):   Operates identically to compareTo(Float) if obj is of class Float. Otherwise, throws a ClassCastException. (Added by Java 2)

double doubleValue( ):   Returns the value of the invoking object as a double.

boolean equals(Object FloatObj):   Returns true if the invoking Float object is equivalent to FloatObj. Otherwise, it returns false.

static int floatToIntBits(float num):    Returns the IEEE-compatible, single-precision bit pattern that corresponds to the num.

float floatValue( ):   Returns the value of the invoking object as a float.

int hashCode( ):   Returns the hash code for the invoking object.

static float intBitsToFloat(int num):  Returns float equivalent of the IEEE-compatible, single-precision bit pattern specified by num.

int intValue( ):   Returns the value of the invoking object as an int.

boolean isInfinite( ):   Returns true if the invoking object contains an infinite value. Otherwise, it returns false.

static boolean isInfinite(float num):  Returns true if num specifies an infinite value. Otherwise, it returns false.

boolean isNaN( ):   Returns true if the invoking object contains a value that is not a number. Otherwise, it returns false.

static boolean isNaN(float num):   Returns true if num specifies a value that is not a number. Otherwise, it returns false.

long longValue( ):   Returns the value of the invoking object as a long.

static float parseFloat(String str) throws NumberFormatException:  Returns the float equivalent of the number contained in the string specified by str using radix 10. (Added by Java 2)

short shortValue( ):   Returns the value of the invoking object as a short.

String toString( ):   Returns the string equivalent of the invoking object.

static String toString(float num):   Returns the string equivalent of the value specified by num.

static Float valueOf(String str) throws NumberFormatException:  Returns the Float object that contains the value specified by the string in str.


The Methods Defined by Double

byte byteValue( ):   Returns the value of the invoking object as a byte.

static int compare(double num1, double num2):   Compares the values of num1 and num2. Returns 0 if the values are equal. Returns a negative value if num1 is less than num2. Returns a positive value if num1 is greater than num2. (Added by Java 2, version 1.4)

int compareTo(Double d):  Compares the numerical value of the invoking object with that of d. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by Java 2)

int compareTo(Object obj):   Operates identically to compareTo(Double) if obj is of class Double. Otherwise, throws a ClassCastException. (Added by Java 2)

static long doubleToLongBits (double num):  Returns the IEEE-compatible, double-precision bit pattern that  corresponds to the num.

double doubleValue( ):  Returns the value of the invoking object as a double.

boolean equals(Object DoubleObj):   Returns true if the invoking Double object is equivalent to DoubleObj. Otherwise, it returns false.

float floatValue( ):   Returns the value of the invoking object as a float.

int hashcode( ):   Returns the hash code for the invoking object.

int intValue( ):   Returns the value of the invoking object as an int.

boolean isInfinite( ):   Returns true if the invoking object contains an infinite value. Otherwise, it returns false.

static boolean isInfinite(double num):   Returns true if num specifies an infinite value. Otherwise, it returns false.

boolean isNaN( ):  Returns true if the invoking object contains a value that is not a number. Otherwise, it returns false.

static boolean isNaN(double num):   Returns true if num specifies a value that is not a number. Otherwise, it returns false.

static double longBitsToDouble (long num):   Returns double equivalent of the IEEE-compatible, double-precision bit pattern specified by num.

long longValue( ):   Returns the value of the invoking object as a long.

static double parseDouble(String str) throws NumberFormatException:  Returns the double equivalent of the number contained in the string specified by str using radix 10. (Added by Java 2)

short shortValue( ):  Returns the value of the invoking object as a short.

String toString( ):   Returns the string equivalent of the invoking object.

static String toString(double num):   Returns the string equivalent of the value specified by num.

static Double valueOf(String str) throws NumberFormatException:  Returns a Double object that contains the value specified by the string in str.


The following example creates two Double objects—one by using a double value and the other by passing a string that can be parsed as a double:

  class DoubleDemo {
    public static void main(String args[]) {
      Double d1 = new Double(3.14159);
      Double d2 = new Double("314159E-5");

      System.out.println(d1 + " = " + d2 + " -> " + d1.equals(d2));
    }
  }

As you can see from the following output, both constructors created identical Double
instances, as shown by the equals( ) method returning true:

  3.14159 = 3.14159 –> true


Understanding isInfinite( ) and isNaN( )

Float and Double provide the methods isInfinite( ) and isNaN( ), which help when manipulating two special double and float values. These methods test for two unique values defined by the IEEE floating-point specification: infinity and NaN (not a number). isInfinite( ) returns true if the value being tested is infinitely large or small in magnitude. isNaN( ) returns true if the value being tested is not a number.

The following example creates two Double objects; one is infinite, and the other is not a number:

  // Demonstrate isInfinite() and isNaN()
  class InfNaN {
    public static void main(String args[]) {
      Double d1 = new Double(1/0.);
      Double d2 = new Double(0/0.);

      System.out.println(d1 + ": " + d1.isInfinite() + ", " 
                         + d1.isNaN());
      System.out.println(d2 + ": " + d2.isInfinite() + ", " 
                         + d2.isNaN());
    }
  }

This program generates the following output:

  Infinity: true, false
  NaN: false, true


Byte, Short, Integer, and Long

The Byte, Short, Integer, and Long classes are wrappers for byte, short, int, and long integer types, respectively. Their constructors are shown here:

      Byte(byte num)
      Byte(String str) throws NumberFormatException

      Short(short num)
      Short(String str) throws NumberFormatException

      Integer(int num)
      Integer(String str) throws NumberFormatException

      Long(long num)
      Long(String str) throws NumberFormatException

As you can see, these objects can be constructed from numeric values or from strings that contain valid whole number values.

The methods defined by these classes are shown in Tables 14-3 through 14-6. As you can see, they define methods for parsing integers from strings and converting strings back into integers. Variants of these methods allow you to specify the radix, or numeric base, for conversion. Common radixes are 2 for binary, 8 for octal, 10 for decimal, and 16 for hexadecimal.

The following constants are defined:

MIN_VALUE               Minimum value
MAX_VALUE             Maximum value
TYPE                           The Class object for byte, short, int, or long


The Methods Defined by Byte

byte byteValue( ):   Returns the value of the invoking object as a byte.

int compareTo(Byte b):   Compares the numerical value of the invoking object with that of b. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value.(Added by Java 2)

int compareTo(Object obj):   Operates identically to compareTo(Byte) if obj is of class Byte. Otherwise, throws a ClassCastException. (Added by Java 2)

static Byte decode(String str) throws NumberFormatException:  Returns a Byte object that contains the value specified by the string in str.

double doubleValue( ):  Returns the value of the invoking object as a double.

boolean equals(Object ByteObj):   Returns true if the invoking Byte object is equivalent to ByteObj. Otherwise, it returns false.

float floatValue( ):  Returns the value of the invoking object as a float.

int hashCode( ):  Returns the hash code for the invoking object.

int intValue( ):   Returns the value of the invoking object as an int.

long longValue( ):  Returns the value of the invoking object as a long.

static byte parseByte(String str) throws NumberFormatException:   Returns the byte equivalent of the number contained in the string specified by str using radix 10.

static byte parseByte (String str, int radix) throws NumberFormatException: Returns the byte equivalent of the number contained in the string specified by str using the specified radix.
    

short shortValue( ):   Returns the value of the invoking object as a short.

String toString( ):   Returns a string that contains the decimal equivalent of the invoking object.

static String toString(byte num):   Returns a string that contains the decimal equivalent of num.

static Byte valueOf(String str) throws NumberFormatException:  Returns a Byte object that contains the value specified by the string in str.

static Byte valueOf (String str, int radix) throws NumberFormatException:  Returns a Byte object that contains the value specified by the string in str using the specified radix.



The Methods Defined by Short

byte byteValue( ):   Returns the value of the invoking object as a byte.

int compareTo(Short s):  Compares the numerical value of the invoking object with that of s. Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. (Added by Java 2)

int compareTo(Object obj):   Operates identically to compareTo(Short) if obj is of class Short. Otherwise, throws a ClassCastException. (Added by Java 2)

static Short decode(String str) throws NumberFormatException:   Returns a Short object that contains the value specified by the string in str.

double doubleValue( ):   Returns the value of the invoking object as a double.

boolean equals(Object ShortObj):   Returns true if the invoking Integer object is equivalent to ShortObj. Otherwise, it returns false.

float floatValue( ):   Returns the value of the invoking object as a float.

int hashCode( ):   Returns the hash code for the invoking object.

int intValue( ):   Returns the value of the invoking object as an int.

long longValue( ):   Returns the value of the invoking object as a long.

static short parseShort(String str) throws NumberFormatException:  Returns the short equivalent of the number contained in the string specified by str using radix 10.

static short parseShort (String str, int radix) throws NumberFormatException: Returns the short equivalent of the number contained in the string specified by str using the specified radix.


short shortValue( ):  Returns the value of the invoking object as a short.

String toString( ):   Returns a string that contains the decimal equivalent of the invoking object.

static String toString(short num):   Returns a string that contains the decimal equivalent of num.

static Short valueOf(String str) throws NumberFormatException:  Returns a Short object that contains the value specified by the string in str using radix 10.

static Short valueOf (String str, int radix) throws NumberFormatException:  Returns a Short object that contains the value specified by the string in str using the specified radix.

No comments:

Post a Comment