0% found this document useful (0 votes)
5 views

Wrapper Classes

Uploaded by

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

Wrapper Classes

Uploaded by

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

Wrapper Class

Kuldeep Yogi
Banasthali University
Overview of java.lang
 Provides classes that are considered
fundamental to the design of the Java
programming language.

 The java.lang package does NOT have to


be imported

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.

 StringBuffer objects can be used almost


anywhere that String can be used – but they
are mutable.

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.

double is a primitive Double is a class.


boolean is a primitive Boolean is a class.
long is a primitive Long is a class.
The wrapper classes for int and char are different.

int is a primitive Integer is a class.


char is a primitive Character is a class.

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);

 All wrapper classes EXCEPT Character and Void can be


instantiated using a String argument.
Integer age = new Integer(“40”);
Double num = new Double(“8.2”);
Boolean isDone = new Boolean(“true”);
 The Void wrapper class cannot be instantiated.

12
Wrapper Class Methods

 Wrapper classes all contain various methods


that help convert from the associated type to
another type.
Integer num = new Integer(4);
float flt = num.floatValue();
//stores 4.0 in flt

Double dbl = new Double(8.2);


int val = dbl.intValue();
//stores 8 in val
13
More Wrapper Class
Methods
 Some Wrapper classes also contain methods
that will compare the value of two objects of
that class.
Integer num1 = new Integer(4);
Integer num2 = new Integer(11);
if(num1.compareTo(num2)< 0)
//executes if num1 < num2

14
Wrapper Class Methods

 Wrapper classes also contain static methods


that help manage the associated type
 Each numeric class contains a method to
convert a representation in a String to the
associated primitive:
int num1 = Integer.parseInt(“3”);
double num2 = Double.parseDouble(“4.7”);
long num3 = Long.parseLong(“123456789”);

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

 The java.lang package


 contains essential classes needed by Java
 does NOT need to be imported
 the String and StringBuffer classes
 the Math class
 the wrapper classes

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.

2. What is unique about the java.lang package?


a. None of the classes in the java.lang package have methods.
b. All of the classes in the java.lang package are abstract.
c. This is a trick question. There is nothing unique about the
java.lang package.
d. The java.lang package is automatically imported.

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.

4. Which of the following statements about the wrapper classes is false?


a. For every primitive data type, there is a corresponding wrapper class.
b. An object of a wrapper class can be used in any situation where a primitive
value will not suffice, for example, in a method call that only accepts object
parameters.
c. Wrapper classes contain a variety of useful fields and methods for working
with primitive data. These are usually static/class methods or fields.
d. Wrapper classes wrap a border around your programs to make them look
professional.

20
Questions Cont…
5. Which of the following is NOT a Wrapper class?
a. Character
b. Boolean
c. Float
d. Int

6. What does the following code do?


num = Double.parseDouble(str);
a. Returns a String with the value of the Double object.
b. Returns a new double initialized to the value represented by
the specified String
c. Returns a new Double initialized to the value represented by
the specified String
d. None of the above.

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.

8. Which of the following is invalid?


a. Integer nine = new Integer(9);
b. Double pi = new Double(“3.14”);
c. Character letter = new Character(‘&’);
d. All are valid.

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…

11. The Math Class


a. can solve mathematical equations and inequalities.
b. provides objects that represent mathematical equations and
inequalities.
c. contains a variety of static constants and methods that are
mathematical in nature.
d. All of the above.

12. True or False, an object of the Math class does


NOT need to be instantiated.
a. true
b. false

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

Don’t forget to turn your answer sheet in.

You might also like