Java: Primitive Type Wrapper Methods for Number and Character Types, Slides of Java Programming

An overview of the methods available for the java primitive type wrappers of number (byte, short, integer, long) and character types. The methods include value conversion, parsing strings to numbers, and comparison. The document also includes examples and explanations of each method.

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Primitive Type Wrapper
Number
Byte , Short, Integer, and Long:METHODS
adrish.b@ardentcollaboratio
ns.com
http://www.ardentcollaborati
ons.com
211
Method
Description
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.
int
compareTo(Object obj)
Operates identically to
compareTo(Byte) if obj is of class
Byte. Otherwise, throws a
ClassCastException.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Java: Primitive Type Wrapper Methods for Number and Character Types and more Slides Java Programming in PDF only on Docsity!

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

211

Method Description

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. int compareTo(Object obj) Operates identically to compareTo(Byte) if obj is of class Byte. Otherwise, throws a ClassCastException.

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

212

Method Description

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 invokingobject as a float.

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

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

214

Method Description

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.

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

215

Method Description

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

int compareTo(Short s ) 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. int compareTo(Object obj) Operates identically to compareTo( Short ) if obj is of class Short. Otherwise, throws a ClassCastException.

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

217

Method Description

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

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

218

Method Description

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. 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.

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

220

Method Description

static Integer decode(String str) throws NumberFormatException

Returns a Integer 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 Integer Obj) Returns true if the invoking Short object is equivalent to Integer Obj. Otherwise, it returns false. float floatValue( ) Returns the value of the invokingobject as a float.

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

static int lowestOneBit(int num)

Method Description

static Integer decode(String str) throws NumberFormatException

Returns a Integer 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 Integer Obj) Returns true if the invoking Short object is equivalent to Integer Obj. Otherwise, it returns false. float floatValue( ) Returns the value of the invokingobject as a float.

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

static int lowestOneBit(int num) Determines the number of high-order zero bits that precede the first high-order set bit in num.If num is zero,32 is returned.

Number

 Byte , Short, Integer, and Long:METHODS

adrish.b@ardentcollaboratio

221

Method Description

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

static Integer getInteger(String propertyName)

Returns the value associated with the environmental property specified by propertyName. A null is returned on failure.

static Integer getInteger(String propertyName, int default)

Returns the value associated with the environmental property specified by propertyName. The value of default is returned on failure.

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

static int parseInt (String str) throws NumberFormatException

Returns the integer equivalent of the number contained in the string specified by str using radix 10.

static int parseInt (String str, int radix) throws NumberFormatException

Returns the integer equivalent of the number contained in the string specified by str using the specified radix

Number

 Byte , Short, Integer, and Long: METHODS

adrish.b@ardentcollaboratio

223

Method Description

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

int compareTo( Long l ) 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. int compareTo(Object obj) Operates identically to compareTo(Long) if obj is of class Long. Otherwise, throws a ClassCastException. static int bitCount(int num) Returns the number of set bits in num.

Number

 Byte , Short, Integer, and Long: METHODS

adrish.b@ardentcollaboratio

224

Method Description

static Integer decode(String str) throws NumberFormatException

Returns a Integer 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 Integer Obj) Returns true if the invoking Short object is equivalent to Integer Obj. Otherwise, it returns false. float floatValue( ) Returns the value of the invokingobject as a float.

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

static int lowestOneBit(int num)

Method Description

static Long decode(String str) throws NumberFormatException

Returns a Long 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 Long Obj) Returns true if the invoking Short object is equivalent to Long Obj. Otherwise, it returns false. float floatValue( ) Returns the value of the invokingobject as a float.

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

static long lowestOneBit(long num) Determines the number of high-order zero bits that precede the first high-order set bit in num.If num is zero,32 is returned.

Number

 Byte , Short, Integer, and Long: METHODS

adrish.b@ardentcollaboratio

226

Method Description

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 Integer valueOf(String str) throws NumberFormatException

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

Returns a Integer object that contains the value specified by the string in str using the specified radix.

Converting Numbers to and from Strings

 The Byte, Short, Integer, and Long classes provide the

parseByte(),parseShort( ), parseInt( ), and parseLong( )

methods, respectively.

 These methods return byte ,short,int and long equivalent of the input string

respectively.

adrish.b@ardentcollaboratio

227

Example of parseInt()

This program sums a list of numbers entered by the user. It converts the string representation of each number into an int using parseInt()

do {

str = br.readLine();

try {

i = Integer.parseInt(str);

} catch(NumberFormatException e) {

System.out.println("Invalid format");

i = 0;

sum += i;

System.out.println("Current sum is: " + sum);

} while(i != 0);

adrish.b@ardentcollaboratio

229

Converting Numbers to and from Strings

To convert a whole number into a decimal string, use the versions of toString( )

defined in the Byte, Short, Integer, or Long classes.

The Integer and Long classes also provide the methods toBinaryString( ),

toHexString( ), and toOctalString( ), which convert a value into a binary,

hexadecimal, or octal string, respectively.

adrish.b@ardentcollaboratio

230