Java lang wrapper classes
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java
Java Wrapper Classes
Each of Java’s eight primitive data types has a class dedicated to it. These are known as wrapper classes because they «wrap» the primitive data type into an object of that class. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.
The wrapper classes in java servers two primary purposes.
- To provide a mechanism to ‘wrap’ primitive values in an object so that primitives can do activities reserved for the objects like being added to ArrayList, Hashset, HashMap etc. collection.
- To provide an assortment of utility functions for primitives like converting primitive types to and from string objects, converting to various bases like binary, octal or hexadecimal, or comparing various objects.
The following two statements illustrate the difference between a primitive data type and an object of a wrapper class:
int x = 25; Integer y = new Integer(33);
The first statement declares an int variable named x and initializes it with the value 25. The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable y.
Below table lists wrapper classes in Java API with constructor details.
Primitive | Wrapper Class | Constructor Argument |
---|---|---|
boolean | Boolean | boolean or String |
byte | Byte | byte or String |
char | Character | char |
int | Integer | int or String |
float | Float | float, double or String |
double | Double | double or String |
long | Long | long or String |
short | Short | short or String |
Below is wrapper class hierarchy as per Java API
As explain in above table all wrapper classes (except Character) take String as argument constructor. Please note we might get NumberFormatException if we try to assign invalid argument in the constructor. For example to create Integer object we can have the following syntax.
Integer intObj = new Integer (25); Integer intObj2 = new Integer ("25");
Here in we can provide any number as string argument but not the words etc. Below statement will throw run time exception (NumberFormatException)
Integer intObj3 = new Integer ("Two");
The following discussion focuses on the Integer wrapperclass, but applies in a general sense to all eight wrapper classes.
The most common methods of the Integer wrapper class are summarized in below table. Similar methods for the other wrapper classes are found in the Java API documentation.
returns a signed decimal integer value equivalent to string s
Let’s see java program which explains few wrapper classes methods.
package WrapperIntro; public class WrapperDemo < public static void main (String args[])< Integer intObj1 = new Integer (25); Integer intObj2 = new Integer ("25"); Integer intObj3= new Integer (35); //compareTo demo System.out.println("Comparing using compareTo Obj1 and Obj2: " + intObj1.compareTo(intObj2)); System.out.println("Comparing using compareTo Obj1 and Obj3: " + intObj1.compareTo(intObj3)); //Equals demo System.out.println("Comparing using equals Obj1 and Obj2: " + intObj1.equals(intObj2)); System.out.println("Comparing using equals Obj1 and Obj3: " + intObj1.equals(intObj3)); Float f1 = new Float("2.25f"); Float f2 = new Float("20.43f"); Float f3 = new Float(2.25f); System.out.println("Comparing using compare f1 and f2: " +Float.compare(f1,f2)); System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3)); //Addition of Integer with Float Float f = intObj1.floatValue() + f1; System.out.println("Addition of intObj1 and f1: "+ intObj1 +"+" +f1+" wrapper class image 2" style="max-width:100%;display:block;height:auto;border: 2px solid silver;">
valueOf (), toHexString(), toOctalString() and toBinaryString() Methods:
This is another approach to creating wrapper objects. We can convert from binary or octal or hexadecimal before assigning a value to wrapper object using two argument constructor. Below program explains the method in details.
package WrapperIntro; public class ValueOfDemo < public static void main(String[] args) < Integer intWrapper = Integer.valueOf("12345"); //Converting from binary to decimal Integer intWrapper2 = Integer.valueOf("11011", 2); //Converting from hexadecimal to decimal Integer intWrapper3 = Integer.valueOf("D", 16); System.out.println("Value of intWrapper Object: "+ intWrapper); System.out.println("Value of intWrapper2 Object: "+ intWrapper2); System.out.println("Value of intWrapper3 Object: "+ intWrapper3); System.out.println("Hex value of intWrapper: " + Integer.toHexString(intWrapper)); System.out.println("Binary Value of intWrapper2: "+ Integer.toBinaryString(intWrapper2)); >>
- Each of primitive data types has dedicated class in java library.
- Wrapper class provides many methods while using collections like sorting, searching etc.
Follow us on Facebook and Twitter for latest update.
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types ( int , boolean , etc..) as objects.
The table below shows the primitive type and the equivalent wrapper class:
Primitive Data Type Wrapper Class byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
Sometimes you must use wrapper classes, for example when working with Collection objects, such as ArrayList , where primitive types cannot be used (the list can only store objects):
Example
ArrayList myNumbers = new ArrayList(); // Invalid
ArrayList myNumbers = new ArrayList(); // Valid
Creating Wrapper Objects
To create a wrapper object, use the wrapper class instead of the primitive type. To get the value, you can just print the object:
Example
Since you're now working with objects, you can use certain methods to get information about the specific object.
For example, the following methods are used to get the value associated with the corresponding wrapper object: intValue() , byteValue() , shortValue() , longValue() , floatValue() , doubleValue() , charValue() , booleanValue() .
This example will output the same result as the example above:
Example
Another useful method is the toString() method, which is used to convert wrapper objects to strings.
In the following example, we convert an Integer to a String , and use the length() method of the String class to output the length of the "string":
Example