Java Wrapper Classes
Java Wrapper Classes
Description
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
can do activities reserved for the objects like being added to ArrayList, Hashset,
primitive types to and from string objects, converting to various bases like
The following two statements illustrate the difference between a primitive data
int x = 25;
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.
As explain in above table all wrapper classes (except Character) take String as
Here in we can provide any number as string argument but not the words etc.
table. Similar methods for the other wrapper classes are found in the Java API
documentation.
Method Purpose
int compareTo(int i) Compares the numerical value of the invoking object with that of i.
num2) equal. Returns a negative value if num1 is less than num2. Returns a
boolean equals(Object intObj) Returns true if the invoking Integer object is equivalent to intObj.
Let’s see java program which explain few wrapper classes methods.
1. package WrapperIntro;
2.
3.
4.
6.
7.
8.
10.
12.
14.
16.
17. //compareTo demo
18.
tObj2));
20.
tObj3));
22.
23.
24.
26.
28.
30.
31.
32.
34.
36.
38.
40.
41. System.out.println("Comparing using compare f1 and f3: " +Float.compare(f1,f3));
42.
43.
44.
46.
48.
50.
51. }
52.
53.
54.
55. }
Output
valueOf (), toHexString(), toOctalString() and
toBinaryString() Methods:
This is another approach to create wrapper objects. We can convert from binary or
octal or hexadecimal before assigning value to wrapper object using two argument
1. package WrapperIntro;
2.
3.
4.
6.
7.
8.
10.
12.
14.
16.
18.
21.
22.
24.
26.
28.
30.
32.
33. }
34.
35. }
Output
Summary
Each of primitive data types has dedicated class in java library.
Wrapper class provides many methods while using collections like sorting,
searching etc.
Source: http://www.w3resource.com/java-tutorial/java-wrapper-classes.php