Wrapper Classes
Wrapper Classes
Kuldeep Yogi
Banasthali University
Overview of java.lang
Provides classes that are considered
fundamental to the design of the Java
programming language.
2
Classes of java.lang
The following is a partial list of classes found in the java.lang package.
Object
String
StringBuffer
System
The “Wrapper” Classes (list shown later)
Math
3
The import Declaration
All classes of the java.lang package are imported
automatically into all programs
It's as if all programs contain the following line:
import java.lang.*;
That's why we didn't have to import the System or String
classes explicitly in earlier programs
The Scanner class, on the other hand, is part of the
java.util package, and therefore must be imported
4
The String and StringBuffer
Classes
Because strings are so common, we deal
with them in a separate unit.
5
The Math Class
The Math class is part of the java.lang package
The Math class contains methods that perform various
mathematical functions
These include:
Absolute value
Exponentiation
Square root
Trigonometric functions
Rounding
Random numbers
And More …
6
The Math Class
The methods of the Math class are all static
methods (also called class methods)
Static methods can be invoked through the class
name – no object of the Math class is ever
needed.
value = Math.cos(angle);//returns the cosine of angle
root = Math.sqrt(num);//returns the square root of num
dist = Math.abs(val);//returns the absolute value of val
small = Math.min(var1, var2);//returns the smaller
value
7
The Math Class
The Math class also includes two symbolic
constants (which are also static)
Math.E
The double value that is closer than any other to e, the
base of the natural logarithms.
Math.PI
The double value that is closer than any other to pi, the
ratio of the circumference of a circle to its diameter.
8
Wrapper Classes
The java.lang package contains wrapper classes
that correspond to each primitive type:
Primitive Type Wrapper Class Except for int and char,
byte Byte the wrapper class
short Short name is exactly the
same as the primitive
int Integer
type name EXCEPT
long Long that it starts with a
float Float capital letter.
double Double
char Character double is a primitive
boolean Boolean Double is a class.
void Void
9
Wrapper Class Names
Except for int and char, the wrapper class name is exactly the same
as the primitive type name EXCEPT that it starts with a capital letter.
10
Wrapper Classes
An object of a wrapper class can be used in any
situation where a primitive value will not suffice
For example, some objects serve as containers
of other objects. Primitive values could not be
stored in such containers, but wrapper objects
could be.
Another example is that some methods take an
Object as a parameter. So a primitive cannot be
used, but an object of any class can be used.
11
Instantiating Wrapper
Classes
All wrapper classes can be instantiated using the
corresponding primitive type as an argument.
Integer age = new Integer(40);
Double num = new Double(8.2);
Character ampersand = new Character(‘&’);
Boolean isDone = new Boolean(false);
12
Wrapper Class Methods
14
Wrapper Class Methods
15
Wrapper Class Constants
The wrapper classes often contain useful
constants as well
The Integer class contains MIN_VALUE and
MAX_VALUE which hold the smallest and
largest int values
Other numeric classes will also have
MIN_VALUE and MAX_VALUE which hold the
smallest and largest value for their
corresponding types.
16
Autoboxing
Autoboxing is the automatic conversion of a
primitive value to a corresponding wrapper object:
Integer obj;
int num = 42;
obj = num;
The assignment creates the appropriate Integer
object
The reverse conversion (called unboxing) also
occurs automatically as needed
17
Summary
18
Questions
1. The java.lang package contains classes that
a. are considered essential for almost every program.
b. assist in translating program from English to other languages
(i.e. Spanish.).
c. assist in translating Java programs into other computer
languages (i.e. C++).
d. can allow your programs to recognize vocal commands.
19
Questions Cont…
3. The Object class
a. contains no methods.
b. is a superclass of every other class.
c. is required when any program interacts with physical objects.
d. is abstract – it cannot be instantiated.
20
Questions Cont…
5. Which of the following is NOT a Wrapper class?
a. Character
b. Boolean
c. Float
d. Int
21
Questions Cont…
7. What is the following?
Long.MAX_VALUE
a. A method that will compare two Long objects and return the
larger one.
b. A method that will compare two long primitives and return the
larger one.
c. A symbolic constant from the Long class that is holding the
maximum value a long (or a Long) can have.
d. None of the above.
22
Questions Cont…
9. Which wrapper class cannot be instantiated?
a. Boolean
b. Integer
c. Character
d. None of the above.
10. Autoboxing is
a. two automobiles boxing – like in the Dodge truck
commercial.
b. the automatic drawing of window’s in a box-like shape.
c. the automatic conversion of a primitive value to a
corresponding wrapper object.
d. None of the above.
23
Questions Cont…
24
Questions Cont…
13. What is the following?
Math.PI
a. A method that will return a double that represents Pi.
b. A symbolic constant that is holding the double value that
represents Pi.
c. A symbolic constant referencing a Double object that
represents Pi.
d. None of the above
14. Which of the following would return the absolute value of the
parameter?
a. Math.cos(num)
b. Math.sqrt(num)
c. Math.abs(num)
d. None of the above
25
The End