Lecture 2.2.1
Lecture 2.2.1
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
2
Wrapper Classes
Primitive data types are not used as objects in Java
However, many Java methods require the use of objects as arguments. For example, the
add(object) method in the ArrayList
Java offers a convenient way to incorporate, or wrap, a primitive data type into an object.
Corresponding class is called a Wrapper Class
For example, wrapping int into the Integer class, and wrapping double into the Double class
Most wrapper class names for a primitive type are the same as the primitive data type name with
the first letter capitalized. The exceptions are Integer and Character.
Java provides Boolean, Character, Double, Float, Byte, Short, Integer, and Long wrapper classes for
primitive data types
These classes are grouped in the java.lang package.
Inheritance Hierarchy of Wrapper Classes
Double and Float
Double and Float
Constructors
Double(double num)
Double(String str) throws NumberFormatException
Float(double num)
Float(float num)
Float(String str) throws NumberFormatException
Constants ( static )
Methods in java.lang.Double
Integer
Constructors
Integer(int value)
Integer(String s)
Constants (static)
Character
Constructors
Character(char ch)
Constants (static)
Methods in java.lang.Character
Boolean
Constructors
Boolean(boolean boolValue)
Boolean(String boolString)
Methods in java.lang.Boolean
int compareTo(Boolean b)
boolean equals(Object boolObj)
static boolean parseBoolean(String str)
String toString( )
static String toString(boolean boolVal)
static Boolean valueOf(String boolString)
Boxing and Unboxing
class AutoBox {
public static void main(String args[]) {
Integer iOb = 100; // autobox an int
int i = iOb; // auto-unbox
System.out.println(i + " " + iOb);
}
}
Reference Links:
https://www.javatpoint.com/post/java-character
https://www.javatpoint.com/wrapper-class-in-java
https://www.javatpoint.com/java-boolean
Video Link:
https://youtu.be/9ch_rkRwk1M
https://youtu.be/RBHtgLSUiw0
THANK YOU