- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain wrapper classes in Java?
Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wraps primitive datatypes within them. Following is the list of primitive datatypes and their respective classes −
Primitive datatype | Wrapper class |
---|---|
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
Following Java example accepts various primitive variables from the user and creates their respective wrapper classes.
Example
import java.util.Scanner; public class WrapperClassesExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int i = sc.nextInt(); //Wrapper class of an integer Integer obj1 = new Integer(i); System.out.println("Enter a long value: "); long l = sc.nextLong(); //Wrapper class of a long Long obj2 = new Long(l); System.out.println("Enter a float value: "); float f = sc.nextFloat(); //Wrapper class of a float Float obj3 = new Float(f); System.out.println("Enter a double value: "); double d = sc.nextDouble(); //Wrapper class of a double Double obj4 = new Double(d); System.out.println("Enter a boolean value: "); boolean bool = sc.nextBoolean(); //Wrapper class of a boolean Boolean obj5 = new Boolean(bool); System.out.println("Enter a char value: "); char ch = sc.next().toCharArray()[0]; //Wrapper class of a boolean Character obj6 = new Character(ch); System.out.println("Enter a byte value: "); byte by = sc.nextByte(); //Wrapper class of a boolean Byte obj7 = new Byte(by); System.out.println("Enter a short value: "); short sh = sc.nextShort(); //Wrapper class of a boolean Short obj8 = new Short(sh); } }
Output
Enter an integer value: 254 Enter a long value: 4444445455454 Enter a float value: 12.324 Enter a double value: 1236.22325 Enter a boolean value: true Enter a char value: c Enter a byte value: 125 Enter a short value: 14233
Following are some important points on wrapper classes.
- At the time of instantiation these classes accept a primitive datatype directly, or in the form of String.
Int i = 20; Integer obj = new Integer(i); Or, Integer obj = new Integer("20");
- Wrapper classes provides methods to, convert primitive datatypes within them to String objects and, to compare them with other objects etc.
Example
import java.util.Scanner; public class WrapperExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int i = sc.nextInt(); //Wrapper class of an integer Integer obj = new Integer(i); //Converting Integer object to String String str = obj.toString(); System.out.println(str); } }
Output
Enter an integer value: 124 124
- Using wrapper classes, you can also add primitive datatypes to various Collection objects such as ArrayList, HashMap etc. You can also pass primitive values over network using wrapper classes.
Example
import java.util.Scanner; public class WrapperExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int i = sc.nextInt(); //Wrapper class of an integer Integer obj = new Integer(i); //Converting Integer object to String String str = obj.toString(); System.out.println(str); //Comparing with other object int result = obj.compareTo(new Integer("124")); if(result==0) { System.out.println("Both are equal"); }else{ System.out.println("Both are not equal"); } } }
Output
Enter an integer value: 1211 1211 Both are not equal
- Related Articles
- What are wrapper classes in Java?
- Primitive Wrapper Classes are Immutable in Java
- What is the need of wrapper classes in Java?
- Sorting Elements of Arrays and Wrapper Classes that Already Implements Comparable in Java
- Explain character classes in Java regular expressions
- Java Float Wrapper Class
- Which packages contain Wrapper class in Java?
- Abstract Classes in Java
- Nested Classes in Java
- Why do we need a wrapper class in Java?
- How to create wrapper objects in JShell in Java 9?
- Explain sub-classes and inheritance in ES6
- What is the difference between String, StringBuffer and StringBuilder classes in Java explain briefly?
- How to convert Wrapper Objects to Primitive Types in Java?
- Static Nested Classes in Java

Advertisements