Using Java s Wrapper Classes

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 10.  Java Classes


Using Java's Wrapper Classes

As you previously learned, all classes are derived from the java.lang.Object class. This is true for all types in the Java language except for primitive types, which include int, boolean, float, double, and so on. For the primitive types, the Java class libraries provide classes called wrapper classes. There is an Integer class that holds ints, there is a Boolean class that holds booleans, and there is a Float class that holds floats. Wrapper classes are important when passing objects across distributed boundaries; distributed objects must all implement the serializable interface; there is no way for a primitive type to be serializable. But we can accomplish the same thing by creating a serializable wrapper around the object.

The wrapper classes have a very unique characteristic in that they are immutable. By definition, the value contained within one of the wrapper classes cannot be altered. Thus, you can be assured that any value you assign to the wrapper class will never change. The drawback to this feature is that if you want to modify the value contained within a wrapper class, you must first extract the value, modify the value, and then create a new wrapper class to hold the modified value. For example, consider the following:

 // Create a new Integer  int n = 5;  Integer myInteger = new Integer( n );  // Increment n  int a = myInteger.intValue();  myInteger = new Integer( ++a ); 

Effectively, this means that to modify an Integer, you must create a new Integer instance and orphan the old one in memory. So, you gain the ability to pass objects across distributed boundaries, but at a price.

Although the wrapper classes are all directly derived from the object class, and there is no unique shared-base class, there is a pattern that you will notice when looking at the wrapper class methods. They all have methods of the following form:

  • xxxValue This method returns the associated primitive type

  • getXXX This method reads the system properties looking for the String value passed to this method and converts that value to the associated primitive type

  • valueOf This method converts a String value to a wrapper class instance

Furthermore, they all provide two constructors: one to build an instance of the class from its associated primitive type, and one to build an instance of the class from a string that will be converted into its associated primitive type.

The Boolean Class

The Boolean class wraps the boolean datatype. The constructors for the Boolean class are shown in Table 10.2.

Table 10.2. Boolean Constructors

Constructor

Description

Boolean(boolean value)

Allocates a Boolean object representing the value argument.

Boolean(String s)

Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string true.

From Table 10.2 you can see that a Boolean wrapper class instance can be built from either a boolean variable or from a String containing either the value true or false.

Table 10.3 shows some of the more useful methods provided by the Boolean class.

Table 10.3. Boolean Method Summary

Constructor

Description

boolean booleanValue()

Returns the value of this Boolean object as a boolean primitive.

boolean equals(Object obj)

Returns true if and only if the argument is not null, and is a Boolean object that represents the same boolean value as this object.

static boolean getBoolean(String name)

Returns true if and only if the system property named by the argument exists and is equal to the string true.

String toString()

Returns a String object representing this Boolean's value

static Boolean valueOf(String s)

Returns a Boolean with a value represented by the specified String.

The Boolean class provides the aforementioned methods (booleanValue(), getBoolean(), and valueOf()) customized for working with the Boolean datatype. In addition, it has a couple more helper methods for converting the Boolean to a String and comparing to Booleans for equality.

The Byte Class

The Byte class wraps the Byte datatype. The constructors for the Byte class are shown in Table 10.4.

Table 10.4. Byte Constructors

Constructor

Description

Byte(byte value)

Constructs a Byte object initialized to the specified byte value.

Byte(String s)

Constructs a Byte object initialized to the value specified by the String parameter.

From Table 10.4 you can see that you can build a Byte wrapper class instance from either a byte variable or from a String containing the decimal value (base 10).

Table 10.5 shows some of the more useful methods provided by the Byte class.

Table 10.5. Byte Method Summary

Method

Description

byte byteValue()

Returns the value of this Byte as a byte.

int compareTo(Byte anotherByte)

Compares two Bytes numerically.

int compareTo(Object o)

Compares this Byte to another Object.

static Byte decode(String nm)

Decodes a String into a Byte.

double doubleValue()

Returns the value of this Byte as a double.

boolean equals(Object obj)

Compares this Object to the specified Object.

float floatValue()

Returns the value of this Byte as a float.

int intValue()

Returns the value of this Byte as an int.

long longValue()

Returns the value of this Byte as a long.

static byte parseByte(String s)

Assuming the specified String represents a byte, returns that byte's value (base 10).

static byte parseByte(String s, int radix)

Assuming the specified String represents a byte, returns that byte's value.

short shortValue()

Returns the value of this Byte as a short.

String toString()

Returns a String object representing this Byte value.

static String toString(byte b)

Returns a new String object representing the specified Byte.

static Byte valueOf(String s)

Assuming the specified String represents a byte, returns a new Byte object initialized to that value.

static Byte valueOf(String s, int radix)

Assuming the specified String represents a byte, returns a new Byte object initialized to that value.

As you can see from Table 10.5, the Byte class is much more sophisticated than the Boolean class because a byte can be interpreted in many ways. The Byte class provides methods to convert the byte to a short, an int, a long, or double value. It provides a set of methods for converting a string to a byte, but to do this it must know the radix of the string value (for example, decimal equals 10, hexadecimal equals 16). Furthermore, it provides two methods for comparing one byte to another byte.

The Character Class

The Character class wraps the char datatype. The constructor for the Character class is shown in Table 10.6.

Table 10.6. Character Constructor

Constructor

Description

Character(char value)

Constructs a Character object and initializes it so that it represents the primitive value argument.

From Table 10.6 you can see that the Character class is an exception to the two constructor summaries described earlier. A character only represents a single character and not a string of characters; therefore, it is not necessary to provide a constructor that accepts a string of characters. Hence, the Character class only provides a constructor that accepts a char datatype.

Table 10.7 shows some of the more useful methods provided by the Character class.

Table 10.7. Character Method Summary

Method

Description

char charValue()

Returns the value of this Character object.

int compareTo(Character anotherCharacter)

Compares two Character objects numerically.

int compareTo(Object o)

Compares this Character to another Object.

static int digit(char ch, int radix)

Returns the numeric value of the character ch in the specified radix.

boolean equals(Object obj)

Compares this Object against the specified Object.

static char forDigit(int digit, int radix)

Determines the character representation for a specific digit in the specified radix.

static int getNumericValue(char ch)

Returns the Unicode numeric value of the character as a non-negative integer.

static int getType(char ch)

Returns a value indicating a character category.

static boolean isDefined(char ch)

Determines if a character has a defined meaning in Unicode.

static boolean isDigit(char ch)

Determines if the specified character is a digit.

static boolean isIdentifierIgnorable(char ch)

Determines if the specified character should be regarded as an ignorable character in a Java identifier or a Unicode identifier.

static boolean isISOControl(char ch)

Determines if the specified character is an ISO control character.

static boolean isJavaIdentifierPart(char ch)

Determines if the specified character can be part of a Java identifier other than the first character.

static boolean isJavaIdentifierStart(char ch)

Determines if the specified character is permissible as the first character in a Java identifier.

static boolean isLetter(char ch)

Determines if the specified character is a letter.

static boolean isLetterOrDigit(char ch)

Determines if the specified character is a letter or digit.

static boolean isLowerCase(char ch)

Determines if the specified character is a lowercase character.

static boolean isSpaceChar(char ch)

Determines if the specified character is a Unicode space character.

static boolean isTitleCase(char ch)

Determines if the specified character is a titlecase character.

static boolean isUnicodeIdentifierPart(char ch)

Determines if the specified character can be part of a Unicode identifier as other than the first character.

static boolean isUnicodeIdentifierStart(char ch)

Determines if the specified character is permissible as the first character in a Unicode identifier.

static boolean isUpperCase(char ch)

Determines if the specified character is an uppercase character.

static boolean isWhitespace(char ch)

Determines if the specified character is white space according to Java.

static char toLowerCase(char ch)

The given character is mapped to its lowercase equivalent; if the character has no lowercase equivalent, the character itself is returned.

String toString()

Returns a String object representing this character's value.

static char toTitleCase(char ch)

Converts the character argument to titlecase.

static char toUpperCase(char ch)

Converts the character argument to uppercase.

Characters can be difficult to work with because they do not necessarily represent a single letter or digit, but can also represent a Unicode character. The Character class provides a plethora of methods for testing, the value and type of a character, as well as methods to manipulate the value of a character. Review Table 10.7 and refer to JavaDoc documentation for additional information.

The Double Class

The Double class wraps the double datatype. The constructors for the Double class are shown in Table 10.8.

Table 10.8. Double Constructors

Constructor

Description

Double(double value)

Constructs a newly allocated Double object that represents the primitive double argument.

Double(String s)

Constructs a newly allocated Double object that represents the floating-point value of type double represented by the String.

As you can see from Table 10.8, Double class follows the pattern I mentioned earlier: It contains two constructors, one that accepts a double-primitive type and one that accepts a string.

Table 10.9 shows some of the more useful methods provided by the Double class.

Table 10.9. Double Method Summary

Method

Description

byte byteValue()

Returns the value of this Double as a byte (by casting to a byte).

int compareTo(Double anotherDouble)

Compares two Double classes numerically.

int compareTo(Object o)

Compares this Double to another Object.

static long doubleToLongBits(double value)

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.

static long doubleToRawLongBits(double value)

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.

double doubleValue()

Returns the double value of this Double.

boolean equals(Object obj)

Compares this Object against the specified Object.

float floatValue()

Returns the float value of this Double.

int intValue()

Returns the integer value of this Double (by casting to an int).

boolean isInfinite()

Returns true if this Double value is infinitely large in magnitude.

static boolean isInfinite(double v)

Returns true if the specified number is infinitely large in magnitude.

boolean isNaN()

Returns true if this Double value is the special Not-a-Number (NaN) value.

static boolean isNaN(double v)

Returns true if the specified number is the special Not-a-Number (NaN) value.

static double longBitsToDouble(long bits)

Returns the double-float corresponding to a given bit represention.

long longValue()

Returns the long value of this Double (by casting to a long).

static double parseDouble(String s)

Returns a new Double initialized to the value represented by the specified String, as performed by the valueOf method of Double class.

short shortValue()

Returns the value of this Double as a short (by casting to a short).

String toString()

Returns a String representation of this Double object.

static String toString(double d)

Creates a String representation of the Double argument.

static Double valueOf(String s)

Returns a new Double object initialized to the value represented by the specified String.

As shown in Table 10.9 the Double class provides a set of methods for converting a Double to another primitive type (byte, int, long, float), modifying the bit structure/interpretation, and a couple methods used for determining the validity of the double value. The isNAN() method returns true if the double value contained in the Double class is a valid number, and the isInfinite() method returns true if the value of the double is equivalent to infinity. A careful point to note about the conversion methods in the Double class is that they perform a cast to the destination primitive type. You always run the risk of losing some of the value when going from a wide type to a narrow type.

The Float Class

The Float class wraps the float datatype. The constructors for the Float class are shown in Table 10.10.

Table 10.10. Float Constructors

Constructor

Description

Float(double value)

Constructs a newly allocated Float object that represents the argument converted from a double to a float.

Float(float value)

Constructs a newly allocated Float object that represents the primitive float argument.

Float(String s)

Constructs a newly allocated Float object that represents the floating-point value of type float represented by the String.

As you can see from Table 10.10, the Float class contains three constructors: one that converts a double to a float, one that accepts a float, and one that accepts a String.

Table 10.11 shows some of the more useful methods provided by the Float class.

Table 10.11. Float Method Summary

Method

Description

byte byteValue()

Returns the value of this Float as a byte (by casting to a byte).

int compareTo(Float anotherFloat)

Compares two Float classes numerically.

int compareTo(Object o)

Compares this Float to another Object.

double doubleValue()

Returns the double value of this Float object.

boolean equals(Object obj)

Compares this Object against some other Object.

static int floatToIntBits(float value)

Returns the bit represention of a single-float value.

static int floatToRawIntBits(float value)

Returns the bit represention of a single-float value.

float floatValue()

Returns the float value of this Float object.

static float intBitsToFloat(int bits)

Returns the single float corresponding to a given bit represention.

int intValue()

Returns theinteger value of this Float (by casting to an int).

boolean isInfinite()

Returns true if this Float value is infinitely large in magnitude.

static boolean isInfinite(float v)

Returns true if the specified number is infinitely large in magnitude.

boolean isNaN()

Returns true if this Float value is Not-a-Number (NaN).

static boolean isNaN(float v)

Returns true if the specified number is the special Not-a-Number (NaN) value.

long longValue()

Returns the long value of this Float (by casting to a long).

static float parseFloat(String s)

Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of the Double class.

short shortValue()

Returns the value of this Float as a short (by casting to a short).

String toString()

Returns a String representation of this Float object.

static String toString(float f)

Returns a String representation for the specified float value.

static Float valueOf(String s)

Returns the floating-point value represented by the specified String.

The Float class provides methods for converting its float value to other data types (byte, short, int, long, double), as well as converting a string of two floats and converting a float to a string. Other methods include validation of the float value to see if it is a number or if it is infinity.

The Integer Class

The Integer class wraps the int datatype. The constructors for the Integer class are shown in Table 10.12.

Table 10.12. Integer Constructors

Constructor

Description

Integer(int value)

Constructs a newly allocated Integer object that represents the primitive int argument.

Integer(String s)

Constructs a newly allocated Integer object that represents the value represented by the String.

The Integer class provides the two standard constructors: one that accepts an int value, and one that accepts a string and converts it to an Integer.

Table 10.13 shows some of the more useful methods provided by the Integer class.

Table 10.13. Integer Method Summary

Method

Description

byte byteValue()

Returns the value of this Integer as a byte.

int compareTo(Integer anotherInteger)

Compares two Integer objects numerically.

int compareTo(Object o)

Compares this Integer to another Object.

static Integer decode(String nm)

Decodes a String into an Integer.

double doubleValue()

Returns the value of this Integer as a double.

boolean equals(Object obj)

Compares this Object to the specified Object.

float floatValue()

Returns the value of this Integer as a float.

static Integer getInteger(String nm)

Determines the Integer value of the system property with the specified name.

static Integer getInteger(String nm, int val)

Determines the Integer value of the system property with the specified name.

static Integer getInteger(String nm, Integer val)

Returns the Integer value of the system property with the specified name.

int intValue()

Returns the value of this Integer as an int.

long longValue()

Returns the value of this Integer as a long.

static int parseInt(String s)

Parses the String argument as a signed decimal integer.

static int parseInt(String s, int radix)

Parses the String argument as a signed integer in the radix specified by the second argument.

short shortValue()

Returns the value of this Integer as a short.

static String toBinaryString(int i)

Creates a String representation of the Integer argument as an unsigned Integer in base 2.

static String toHexString(int i)

Creates a String representation of the Integer argument as an unsigned integer in base 16.

static String toOctalString(int i)

Creates a String representation of the integer argument as an unsigned integer in base 8.

String toString()

Returns a String object representing this Integer's value.

static String toString(int i)

Returns a new String object representing the specified integer.

static String toString(int i, int radix)

Creates a String representation of the first argument in the radix specified by the second argument.

static Integer valueOf(String s)

Returns a new Integer object initialized to the value of the specified String.

static Integer valueOf(String s, int radix)

Returns a new Integer object initialized to the value of the specified String.

The Integer class provides the same methods for converting its int value to the other primitive types (byte, short, long, float, double, and so on), as well as methods to convert the int to a string and a string to an int. It provides some very useful methods for converting an Integer from its natural decimal value to a different radix base: binary, octal, hexadecimal. Remember that these methods exist because doing the conversion manually is very tedious, especially when someone has already done it for you.

The Long Class

The Long class wraps the long datatype. The constructors for the Long class are shown in Table 10.14.

Table 10.14. Long Constructors

Constructor

Description

Long(long value)

Constructs a newly allocated Long object that represents the primitive long argument.

Long(String s)

Constructs a newly allocated Long object that represents the value represented by the String in decimal form.

The Long class provides the two standard constructors: one that accepts a long value, and one that accepts a string and converts it to a Long.

Table 10.15 shows some of the more useful methods provided by the Long class.

Table 10.15. Long Method Summary

Method

Description

byte byteValue()

Returns the value of this Long as a byte.

int compareTo(Long anotherLong)

Compares two Long objects numerically.

int compareTo(Object o)

Compares this Long to another Object.

static Long decode(String nm)

Decodes a String into a Long.

double doubleValue()

Returns the value of this Long as a double.

boolean equals(Object obj)

Compares this Object against the specified Object.

float floatValue()

Returns the value of this Long as a float.

static Long getLong(String nm)

Determines the Long value of the system property with the specified name.

static Long getLong(String nm, long val)

Determines the Long value of the system property with the specified name.

static Long getLong(String nm, Long val)

Returns the Long value of the system property with the specified name.

int intValue()

Returns the value of this Long as an int.

long longValue()

Returns the value of this Long as a long value.

static long parseLong(String s)

Parses the String argument as a signed decimal long.

static long parseLong(String s, int radix)

Parses the String argument as a signed long in the radix specified by the second argument.

short shortValue()

Returns the value of this Long as a short.

static String toBinaryString(long i)

Creates a String representation of the long argument as an unsigned integer in base 2.

static String toHexString(long i)

Creates a String representation of the Long argument as an unsigned integer in base 16.

static String toOctalString(long i)

Creates a String representation of the long argument as an unsigned integer in base 8.

String toString()

Returns a String object representing this Long's value.

static String toString(long i)

Returns a new String object representing the specified integer.

static String toString(long i, int radix)

Creates a String representation of the first argument in the radix specified by the second argument.

static Long valueOf(String s)

Returns a new Long object initialized to the value of the specified String.

static Long valueOf(String s, int radix)

Returns a new Long object initialized to the value of the specified String.

The Long class is virtually identical to the Integer class except that it uses long value-sensitive integer values. It provides methods for converting its long value to the other primitive types (byte, short, int, float, double), as well as methods to convert the long to a string and a string to a long. It provides the same methods for converting a long from its natural decimal value to a different radix base: binary, octal, hexadecimal.

The Short Class

The Short class wraps the short datatype. The constructors for the Short class are shown in Table 10.16.

Table 10.16. Short Constructors

Constructor

Description

Short(short value)

Constructs a Short object initialized to the specified short value.

Short(String s)

Constructs a Short object initialized to the value specified by the String parameter.

The Short class provides the two standard constructors: one that accepts a short value and one that accepts a String and converts it to a Short.

Table 10.17 shows some of the more useful methods provided by the Short class.

Table 10.17. Short Method Summary

Method

Description

byte byteValue()

Returns the value of this Short as a byte.

int compareTo(Object o)

Compares this Short to another Object.

int compareTo(Short anotherShort)

Compares two Shorts numerically.

static Short decode(String nm)

Decodes a String into a Short.

double doubleValue()

Returns the value of this Short as a double.

boolean equals(Object obj)

Compares this Object to the specified Object.

float floatValue()

Returns the value of this Short as a float.

int intValue()

Returns the value of this Short as an int.

long longValue()

Returns the value of this Short as a long.

static short parseShort(String s)

Assuming the specified String represents a Short, returns that Short's value.

static short parseShort(String s, int radix)

Assuming the specified String represents a short, returns that short's value.

short shortValue()

Returns the value of this Short as a short.

String toString()

Returns a String object representing this Short's value.

static String toString(short s)

Returns a new String object representing the specified Short.

static Short valueOf(String s)

Assuming the specified String represents a short, returns a new Short object initialized to that value.

static Short valueOf(String s, int radix)

Assuming the specified String represents a short, returns a new Short object initialized to that value.

The Short class is virtually identical to the Integer and Long classes except that it uses a short value: It provides methods for converting its short value to the other primitive types (byte, int, long, float, double), as well as methods to convert the short to a string and a string to a short.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net