[go: up one dir, main page]

0% found this document useful (0 votes)
9 views22 pages

Lecture 05

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Wrapper classes in java

Mateen Ahmed Abbasi


Department of Computer Science
Khwaja Fareed University Of Engineering and Information Technology
Wrapper classes in java
For each and every fundamental data type
there exist a pre-defined class, Such
predefined class is known as wrapper class.
The purpose of wrapper class is to convert
numeric string data into numerical or
fundamental data.
Cont..
All the wrapper classes (Integer, Long, Byte, Double, Float,
Short) are subclasses of the abstract class Number.
The object of the wrapper class contains or wraps its
respective primitive data type. Converting primitive data
types into object is called boxing, and this is taken care by
the compiler. Therefore, while using a wrapper class you
just need to pass the value of the primitive data type to the
constructor of the Wrapper class.
And the Wrapper object will be converted back to a
primitive data type, and this process is called unboxing.
The Number class is part of the java.lang package.
Cont..
Each of Java's eight primitive data types has a class dedicated to it.
These are known as wrapper classes because they "wrap" the
primitive data type into an object of that class. The wrapper
classes are part of the java.lang package, which is imported by
default into all Java programs.

The primary purpose of wrapper classes in java is.


To provide an variety of utility functions for primitives like
converting primitive types to and from string objects, converting
to various bases like binary, octal or hexadecimal, or comparing
various objects.
Cont..
The following two statements illustrate the difference
between a primitive data type and an object of a
wrapper class:
int x = 25;
Integer y = new Integer(33);
The first statement declares an int variable named x
and initializes it with the value 25. The second
statement instantiates an Integer object. The object is
initialized with the value 33 and a reference to the
object is assigned to the object variable y.
Use of the Wrapper Classes
• Java’s primitive data types (boolean, int, etc.)
are not classes.
• Wrapper classes are used in situations where
objects are required, such as for elements of a
Collection:
List<Integer> a = new ArrayList<Integer>();
methodRequiringListOfIntegers(a);
Value => Object: Wrapper Object Creation
Wrapper.valueOf() takes a value (or
string) and returns an object of that
class:

Integer i1 = Integer.valueOf(42);
Integer i2 = Integer.valueOf(“42”);

Boolean b1 = Boolean .valueOf(true);


Boolean b2 = Boolean .valueOf(“true”);

Long n1 = Long.valueOf(42000000L);
Long n1 = Long.valueOf(“42000000L”);
Object => Value
Each wrapper class Type has a method typeValue to obtain
the object’s value:

Integer i1 = Integer.valueOf(42);
Boolean b1 = Boolean.valueOf(“false”);
System.out.println(i1.intValue());
System.out.println(b1.intValue());

=>
42
false
Why use wrapper classes ?
We know that in java whenever we get input
form user, it is in the form of string value so
here we need to convert these string values in
different datatype (numerical or fundamental
data), for this conversion we use wrapper
classes.
`
Example
class WraperDemo
{
public static void main(String[] args)
{
String s[] = {"10", "20"};
System.out.println("Sum before:"+ s[0] + s[1]); // 1020
int x=Integer.parseInt(s[0]); // convert String to Integer
int y=Integer.parseInt(s[1]); // convert String to Integer
int z=x+y;
System.out.println("sum after: "+z); // 30
}
}
Explanation of Example
In the above example 10 and 20 are store in
String array and next we convert these values
in Integer using "int x=Integer.parseInt(s[0]);"
and "int y=Integer.parseInt(s[1]);" statement
In "System.out.println("Sum before:"+ s[0] +
s[1]);" Statement normally add two string and
output is 1020 because these are String
numeric type not number.
Converting String data into fundamental or
numerical
We know that every command line argument of java program is
available in the main() method in the form of array of object of
string class on String data, one can not perform numerical
operation. To perform numerical operation it is highly desirable
to convert numeric String into fundamental numeric value.
Example
"10" --> numeric String --> only numeric string convert into
numeric type or value.
"10x" --> alpha-numeric type --> this is not conversion.
"ABC" --> non-numeric String no conversion.
"A" --> char String no conversion.
List of Wrapper Classes
Below table lists wrapper classes in Java API with constructor
details.

Primitive Wrapper Class Constructor Argument


boolean Boolean boolean or String
byte Byte byte or String
char Character char
int Integer int or String
float Float float, double or String
double Double double or String
long Long long or String
short Short short or String
Fundamental data type and corresponding
wrapper classes
The following table gives fundamental data type corresponding
wrapper class name and conversion method from numerical
String into numerical values or fundamental value.

Fundamental Wrapper Conversion method from numeric string into


DataType CalssName fundamental or numeric value
byte Byte public static byte parseByte(String)
short Short public static short parseShort(String)
int Integer public static integer parseInt(String)
long Long public static long parseLong(String)
float Float public static float parseFloat(String)
double Double public static double parseDouble(String)
boolean Boolean public static boolean parseBoolean(String)
wrapper class hierarchy as per Java API
The most common methods of the Integer wrapper class
Method Purpose

parseInt(s) returns a signed decimal integer value equivalent to string s


toString(i) returns a new String object representing the integer i
byteValue() returns the value of this Integer as a byte
doubleValue() returns the value of this Integer as a double
floatValue() returns the value of this Integer as a float
intValue() returns the value of this Integer as an int
shortValue() returns the value of this Integer as a short
longValue() returns the value of this Integer as a long
int compareTo(int i) Compares the numerical value of the invoking object with that of i. Returns 0 if
the values are equal. Returns a negative value if the invoking object has a lower
value. Returns a positive value if the invoking object has a greater value.
static int compare(int Compares the values of num1 and num2. Returns 0 if the values are equal.
num1, int num2) Returns a negative value if num1 is less than num2. Returns a positive value if
num1 is greater than num2.
boolean equals(Object Returns true if the invoking Integer object is equivalent to intObj. Otherwise, it
intObj) returns false.
How to use wrapper class methods

All the wrapper class methods are static in nature so


we need to call these method using
class.methodName().
• for Integer: int x=Integer.parseInt(String);
• for float: float x=Float.parseFloat(String);
• For double: double x=Double.parseDouble(String);
Each and every wrapper class contains the following
generalized method for converting numeric String
into fundamental values.
Type conversion in Java
Type conversion in Java
When you assign value of one data type to another,
the two types might not be compatible with each
other. If the data types are compatible, then Java will
perform the conversion automatically known as
Automatic Type Conversion and if not then they need
to be casted or converted explicitly. For example,
assigning an int value to a long variable.
Assigning a value of one type to a variable of another
type is known as Type Casting.
Cont…
In Java, type casting is classified into two types,
Widening Casting(Implicit)

Narrowing Casting(Explicitly done)


Example :Implicit Conversion
public class Test
{
public static void main(String[] args)
{
int i = 100;
long l = i; //no explicit type casting required
float f = l; //no explicit type casting required
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}

}
Narrowing or Explicit type conversion
When you are assigning a larger type value to a variable of smaller
type, then you need to perform explicit type casting.
Example
public class Test
{
public static void main(String[] args)
{
double d = 100.04;
long l = (long)d; //explicit type casting required
int i = (int)l; //explicit type casting required
System.out.println("Double value "+d);
System.out.println("Long value "+l);
System.out.println("Int value "+i);
}}

You might also like