Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Wrapper classes in Java | Pragjyotish College, Lecture notes of Object Oriented Programming

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

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

ekavaria
ekavaria 🇺🇸

4.4

(38)

262 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Wrapper classes in Java
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.
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.
o 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.
o 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.
o Synchronization: Java synchronization works with objects in Multithreading.
o java.util package: The java.util package provides the utility classes to deal with objects.
o 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
boolean
Boolean
char
Character
byte
Byte
short
Short
int
Integer
pf3

Partial preview of the text

Download Wrapper classes in Java | Pragjyotish College and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Wrapper classes in Java

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.

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. o 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. o 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. o Synchronization: Java synchronization works with objects in Multithreading. o java.util package: The java.util package provides the utility classes to deal with objects. o 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

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. System.out.println(a+" "+i+" "+j);
  10. }} 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
  11. //Java program to convert object into primitives
  12. //Unboxing example of Integer to int
  13. public class WrapperExample2{
  14. public static void main(String args[]){
  15. //Converting Integer to int