0% found this document useful (0 votes)
10 views16 pages

Lecture 2.2.1

The document discusses the use of wrapper classes in Java, including Integer, Character, Long, and Boolean, as well as the concepts of autoboxing and unboxing. It explains how primitive data types can be wrapped into objects for use in methods that require object arguments. Additionally, it provides details on the constructors, constants, and methods associated with each wrapper class.

Uploaded by

madangleaming13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Lecture 2.2.1

The document discusses the use of wrapper classes in Java, including Integer, Character, Long, and Boolean, as well as the concepts of autoboxing and unboxing. It explains how primitive data types can be wrapped into objects for use in methods that require object arguments. Additionally, it provides details on the constructors, constants, and methods associated with each wrapper class.

Uploaded by

madangleaming13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)

TOPIC OF PRESENTATION:

Use of wrapper classes in Java- Integer, Character, Long, Boolean.


Autoboxing and Unboxing. (CO 4)

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


•Use of wrapper classes in
Java- Integer, Character, Long,
Boolean. Autoboxing and
Unboxing.

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 )

MAX_VALUE Maximum positive value


MIN_VALUE Minimum positive value
NaN Not a number
POSITIVE_INFINITY Positive infinity
NEGATIVE_INFINITY Negative infinity
TYPE The Class object for float or double
SIZE Number of bits used to represent double type
Double and Float

Methods in java.lang.Double

static int compare(double d1, double d2)


int compareTo(Double anotherDouble)
boolean equals(Object obj)
boolean isInfinite()
boolean isNaN()
static double parseDouble(String s)
String toString()
static Double valueOf(String s)
static String toHexString(double d)

Similar methods in java.lang.Float but with float or Float argument


Integer

Integer
Constructors

Integer(int value)
Integer(String s)

Constants (static)

MAX_VALUE Maximum positive value


MIN_VALUE Minimum positive value
TYPE The Class object for float or double
SIZE Number of bits used to represent int type
Integer
Methods in java.lang.Integer

int compareTo(Integer anotherInteger)


double doubleValue() // similarly intValue(), byteValue(), floatValue(), shortValue()
boolean equals(Object obj)
static int parseInt(String s)
static int parseInt(String s, int radix)
static String toBinaryString(int i) // toHexString(), toOctalString()
String toString()
static String toString(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)
Character

Character
Constructors

Character(char ch)

Constants (static)

MAX_VALUE The largest character value


MIN_VALUE The smallest character value
TYPE The Class object for char
SIZE The number of bits used to represent a char value
Character

Methods in java.lang.Character

static boolean isDefined(char ch)


static boolean isDigit(char ch)
static boolean isLetter(char ch)
static boolean isLetterOrDigit(char ch)
static boolean isLowerCase(char ch)
static boolean isSpaceChar(char ch)
static boolean isUpperCase(char ch)
static boolean isWhitespace(char ch)
static char toLowerCase(char ch)
static char toUpperCase(char ch)
Boolean

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

 Java allows primitive types and wrapper classes to be converted


automatically
 Converting a primitive value to a wrapper object is called Boxing
 The reverse conversion is called Unboxing
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);
}
}

displays 100 100


Summary:

In this session, you were able to :

•Learn about Wrapper Classes.


References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill Companies
3. John P. Flynt Thomson, Java Programming.

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

You might also like