Wrapper classes in Java

Last updated on Dec 29 2022
Prabhas Ramanathan

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing.

Table of Contents

Use of Wrapper classes in Java

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.
• Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
• Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
• Synchronization: Java synchronization works with objects in Multithreading.
• java.util package: The java.util package provides the utility classes to deal with objects.
• Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.
The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes are given below:
Primitive Type Wrapper class

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

Autoboxing

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.
Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects.

Wrapper class Example: Primitive to Wrapper

1. //Java program to convert primitive into objects 
2. //Autoboxing example of int to Integer 
3. public class WrapperExample1{ 
4. public static void main(String args[]){ 
5. //Converting int into Integer 
6. int a=20; 
7. Integer i=Integer.valueOf(a);//converting int into Integer explicitly 
8. Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally 
9. 
10. System.out.println(a+" "+i+" "+j); 
11. }}

Output:
20 20 20

Unboxing

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.

Wrapper class Example: Wrapper to Primitive

1. //Java program to convert object into primitives 
2. //Unboxing example of Integer to int 
3. public class WrapperExample2{ 
4. public static void main(String args[]){ 
5. //Converting Integer to int 
6. Integer a=new Integer(3); 
7. int i=a.intValue();//converting Integer to int explicitly 
8. int j=a;//unboxing, now compiler will write a.intValue() internally 
9. 
10. System.out.println(a+" "+i+" "+j); 
11. }}

 

Output:
3 3 3

Java Wrapper classes Example

1. //Java Program to convert all primitives into its corresponding 
2. //wrapper objects and vice-versa 
3. public class WrapperExample3{ 
4. public static void main(String args[]){ 
5. byte b=10; 
6. short s=20; 
7. int i=30; 
8. long l=40; 
9. float f=50.0F; 
10. double d=60.0D; 
11. char c='a'; 
12. boolean b2=true; 
13. 
14. //Autoboxing: Converting primitives into objects 
15. Byte byteobj=b; 
16. Short shortobj=s; 
17. Integer intobj=i; 
18. Long longobj=l; 
19. Float floatobj=f; 
20. Double doubleobj=d; 
21. Character charobj=c; 
22. Boolean boolobj=b2; 
23. 
24. //Printing objects 
25. System.out.println("---Printing object values---"); 
26. System.out.println("Byte object: "+byteobj); 
27. System.out.println("Short object: "+shortobj); 
28. System.out.println("Integer object: "+intobj); 
29. System.out.println("Long object: "+longobj); 
30. System.out.println("Float object: "+floatobj); 
31. System.out.println("Double object: "+doubleobj); 
32. System.out.println("Character object: "+charobj); 
33. System.out.println("Boolean object: "+boolobj); 
34. 
35. //Unboxing: Converting Objects to Primitives 
36. byte bytevalue=byteobj; 
37. short shortvalue=shortobj; 
38. int intvalue=intobj; 
39. long longvalue=longobj; 
40. float floatvalue=floatobj; 
41. double doublevalue=doubleobj; 
42. char charvalue=charobj; 
43. boolean boolvalue=boolobj; 
44. 
45. //Printing primitives 
46. System.out.println("---Printing primitive values---"); 
47. System.out.println("byte value: "+bytevalue); 
48. System.out.println("short value: "+shortvalue); 
49. System.out.println("int value: "+intvalue); 
50. System.out.println("long value: "+longvalue); 
51. System.out.println("float value: "+floatvalue); 
52. System.out.println("double value: "+doublevalue); 
53. System.out.println("char value: "+charvalue); 
54. System.out.println("boolean value: "+boolvalue); 
55. }}

Output:
—Printing object values—
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true

—Printing primitive values—

byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a

boolean value: true

Custom Wrapper class in Java

Java Wrapper classes wrap the primitive data types, that is why it is known as wrapper classes. We can also create a class which wraps a primitive data type. So, we can create a custom wrapper class in Java.

1. //Creating the custom wrapper class 
2. class tecklearn{ 
3. private int i; 
4. tecklearn(){} 
5. tecklearn(int i){ 
6. this.i=i; 
7. } 
8. public int getValue(){ 
9. return i; 
10. } 
11. public void setValue(int i){ 
12. this.i=i; 
13. } 
14. @Override 
15. public String toString() { 
16. return Integer.toString(i); 
17. } 
18. } 
19. //Testing the custom wrapper class 
20. public class Testtecklearn{ 
21. public static void main(String[] args){ 
22. tecklearn j=new tecklearn(10); 
23. System.out.println(j); 
24. }}

Output:
10

So, this brings us to the end of blog. This Tecklearn ‘Wrapper classes in Java’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Wrapper classes in Java"

Leave a Message

Your email address will not be published. Required fields are marked *