Apr 25

In this primitive wrapper class tutorial, let’s take a look at different API methods & constructors of primitive wrapper classes such as Byte, Short, Long, Float, Double, and Boolean which are used for conversions between strings, primitive data types & wrapper classes/wrapper data types along with suitable examples.

Methods of Integer wrapper class can be found in tutorial Java Integer Wrapper Class Tutorial: API Methods and Constructors.

Methods of Character wrapper class can be found in tutorial Java Character Wrapper Class Tutorial: API Methods and Constructors

API Methods of Primitive Wrapper Classes in java

The following is the list of common methods/functions api implemented by all numeric wrapper classes (subclasses of java.lang.Number):

  • byte byteValue() // Converts the value of wrapper object to the primitive byte type
  • short shortValue() // Converts the value of wrapper object to the primitive short type
  • int intValue() // Converts the value of wrapper object to the primitive int type
  • long longValue() // Converts the value of wrapper object to the primitive long type
  • float floatValue() // Converts the value of wrapper object to the primitive float type
  • double doubleValue() // Converts the value of wrapper object to the primitive double type

Similar to above methods, the Character wrapper class contains char charValue() method which is used to convert the value of Character wrapper object to the primitive char type and the Boolean wrapper class contains boolean booleanValue() method that is useful to convert the value of Boolean wrapper object to the primitive boolean type.

Each numeric wrapper class contains other methods that are useful for converting numbers to and from strings and for converting between number systems.

API: The list of data type conversion methods in the Byte wrapper class:

  • static byte parseByte(String s) // Converts the String object to primitive byte data type
  • static byte parseByte(String s, int radix) // Converts the String representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) to the primitive byte type
  • String toString() // Converts the value of Byte wrapper class object to String
  • static String toString(byte b) // Converts the value of primitive data type byte to String
  • static Byte valueOf(byte b) // Converts the value of primitive byte to Byte wrapper object
  • static Byte valueOf(String s) // Converts the value of String object to Byte wrapper class object
  • static Byte valueOf(String s, int radix) // Converts the value of String (parsed with the value of radix) to Byte wrapper object

API: The list of data type conversion methods in the Short wrapper class:

  • static short parseShort(String s) // Converts the String object to primitive short data type
  • static short parseShort(String s, int radix) // Converts the String representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) to the primitive short type
  • String toString() // Converts the value of Short wrapper class object to String
  • static String toString(short s) // Converts the value of primitive data type short to String
  • static Short valueOf(short s) // Converts the value of primitive short to Short wrapper object
  • static Short valueOf(String s) // Converts the value of String object to Short wrapper class object
  • static Short valueOf(String s, int radix) // Converts the value of String (parsed with the value of radix) to Short wrapper object

API: The list of data type conversion methods in the Long wrapper class:

  • static long parseLong(String s) // Converts the String object to primitive long data type
  • static long parseLong(String s, int radix) // Converts the String representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) to the primitive long type
  • String toString() // Converts the value of Long wrapper class object to String
  • static String toString(long l) // Converts the value of primitive data type long to String
  • static Long valueOf(long l) // Converts the value of primitive long to Long wrapper object
  • static Long valueOf(String s) // Converts the value of String object to Long wrapper class object
  • static Long valueOf(String s, int radix) // Converts the value of String (parsed with the value of radix) to Long wrapper object

API: The list of data type conversion methods in the Float wrapper class:

  • static float parseFloat(String s) // Converts the String object to primitive float data type
  • String toString() // Converts the value of Float wrapper class object to String
  • static String toString(float f) // Converts the value of primitive data type float to String
  • static Float valueOf(float f) // Converts the value of primitive float to Float wrapper object
  • static Float valueOf(String s) // Converts the value of String object to Float wrapper class object

API: The list of data type conversion methods in the Double wrapper class:

  • static double parseDouble(String s) // Converts the String object to primitive double data type
  • String toString() // Converts the value of Double wrapper class object to String
  • static String toString(double d) // Converts the value of primitive data type double to String
  • static Double valueOf(double d) // Converts the value of primitive double to Double wrapper object
  • static Double valueOf(String s) // Converts the value of String object to Double wrapper class object

API: The list of data type conversion methods in the Boolean wrapper class:

  • static boolean parseBoolean(String s) // Converts the String object to primitive boolean data type
  • String toString() // Converts the value of Boolean wrapper class object to String
  • static String toString(boolean b) // Converts the value of primitive data type boolean to String
  • static Boolean valueOf(boolean b) // Converts the value of primitive boolean to Boolean wrapper object
  • static Boolean valueOf(String s) // Converts the value of String object to Boolean wrapper class object

Along with all of the above methods, one more important method/function defined in all types of wrapper classes known as compareTo() which is very useful for sorting of wrapper class objects.

  • Byte wrapper class: int compareTo(Byte anotherByte) // Compares this Byte wrapper class object with anotherByte object numerically.
  • Short wrapper class: int compareTo(Short anotherShort) // Compares this Short wrapper class object with anotherShort object numerically.
  • Long wrapper class: int compareTo(Long anotherLong) // Compares this Long wrapper class object with anotherLong object numerically.
  • Float wrapper class: int compareTo(Float anotherFloat) // Compares this Float wrapper class object with anotherFloat object numerically.
  • Double wrapper class: int compareTo(Double anotherDouble) // Compares this Double wrapper class object with anotherDouble object numerically.
  • Boolean wrapper class: int compareTo(Boolean anotherBoolean) // Compares this Boolean wrapper instance with anotherBoolean object.

Coding Examples for compareTo() method of Java Wrapper Classes:
int compareTo(Long anotherLong) returns

  • the value 0 if this Long wrapper object is equal to the argument anotherLong;
  • a value less than 0 if this Long is numerically less than the argument anotherLong;
  • and a value greater than 0 if this Long is numerically greater than the argument anotherLong(signed comparison).
    Long lng1 = new Long(10);
    Long lng2 = new Long(20);
    System.out.println(" Result: " + lng1.compareTo(lng2) );  
    Result: -1

Constructors of Wrapper Classes in java

Here you can see two different types of constructors implemented by each of the wrapper classes (except Character). The first version of constructor is used to convert primitive data type to its wrapper class object and the second version is used to convert String object to respective wrapper type object.

API: The list of constructors for Byte wrapper class:

  • Byte(byte value) // Constructs a new Byte wrapper class object that represents the specified primitive byte type value
  • Byte(String s) // Constructs a new Byte wrapper class object that represents the specified primitive byte type value indicated by the String parameter

API: The list of constructors for Short wrapper class:

  • Short(short value) // Constructs a new Short wrapper class object that represents the specified primitive short type value
  • Short(String s) // Constructs a new Short wrapper class object that represents the specified primitive short type value indicated by the String parameter

API: The list of constructors for Long wrapper class:

  • Long(long value) // Constructs a new Long wrapper class object that represents the specified primitive long type value
  • Long(String s) // Constructs a new Long wrapper class object that represents the specified primitive long type value indicated by the String parameter

API: The list of constructors for Float wrapper class:

  • Float(float value) // Constructs a new Float wrapper class object that represents the specified primitive float type value
  • Float(String s) // Constructs a new Float wrapper class object that represents the floating-point value of type float indicated by the String parameter
  • Float(double value) // Constructs a new Float wrapper class object that represents the specified primitive double type value converted to type float

API: The list of constructors for Double wrapper class:

  • Double(double value) // Constructs a new Double wrapper class object that represents the specified primitive double type value
  • Double(String s) // Constructs a new Double wrapper class object that represents the floating-point value of type double indicated by the String parameter

API: The list of constructors for Boolean wrapper class:

  • Boolean(boolean value) // Constructs a new Boolean wrapper class object that represents the specified primitive boolean type value
  • Boolean(String s) // Constructs a new Boolean wrapper class object representing the value true if the String argument is not null and is equal, ignoring case, to the string “true”

Example Codes for Java Wrapper Class Constructors:

    Float f = new Float(15.6F);  // Float wrapper object with value 15.6
    Long l = new Long("526");  // Long wrapper object with value 526
    Character c = new Character('@');  // Character object with value @
    Boolean b = new Boolean("TRUE");   // Boolean object with value true
    System.out.printf(" Result: f=%.2f, l=%d, c=%c, b=%b", f, l, c, b );  
    Result: f=15.60, l=526, c=@, b=true

To learn more about output formatting (printf) used in above example program, read the tutorial Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2.

Related posts:

  1. Java Character Wrapper Class Tutorial: API Methods and Constructors
  2. Java Integer Wrapper Class Tutorial: API Methods and Constructors
  3. Java Wrapper Class Tutorial: Explain Java Wrapper Classes with Examples
  4. Is String Mutable in Java? Why Wrapper Classes are Immutable and Final in Java?
  5. Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 2
  6. Java 5 (J2SE 5.0/JDK 1.5) New Features with Examples Tutorial 1
  7. How to fix “Exception in thread main java.lang” Common Exceptions? – Tutorial 1
  8. Explain Most Common Exceptions and Errors in Java with Examples
  9. What is Java? How to download Java (J2SE/JDK/JRE), IDE, Javadocs?
  10. Write a Java Program to Find Reverse of a Given Number and String with Output

written by Seetha Ram Janapala
Tags: boolean constructor, character wrapper class in java, core java for beginners, core java tutorial, features of wrapper class in java, j2se 5.0, java boolean api methods, java byte constructor, java byte methods api, java double constructor, java double methods api, java float api methods, java float constructor, java long constructor, java primitive wrapper types, java short constructor, java wrapper classes and boxing, long api methods, primitive wrapper classes in java, short api methods, wrapper class advantages in java, wrapper class constructors, wrapper class conversions, wrapper class functions, wrapper class methods in java, wrapper classes in java with example, wrapper data types in java, wrapper objects in java, wrapper types in java


One Response to “Java Primitive Wrapper Class Tutorial: API Methods and Constructors”

  1. 1. Java training in chennai Says:

    very good explanation.

Leave a Reply

 
Copyright © 2014 Explain-Java.com. All rights reserved.