Ch 2 Library Classes
Ch 2 Library Classes
1
Blue Ridge Public School
Topics
• Class as a Composite Type
2
Blue Ridge Public School
Class as a Composite Type
• There are 8 primitive data types defined in Java, they
are byte, short, int, long, float, double, char, boolean
8
Blue Ridge Public School
Role of New Keyword
• As mentioned previously, a class provides the blueprints for
objects. So basically, an object is created from a class
• In Java, the new keyword is used to create new objects
• The new operator instantiates a class by allocating memory for
a new object and returning a reference to that memory
• The new operator also invokes the object constructor
9
Blue Ridge Public School
Role of New Keyword – cont.
• The phrase "instantiating a class" means the same thing as
"creating an object.“
• When you create an object, you are creating an "instance" of
a class, therefore "instantiating" a class
• The new operator requires a single, postfix argument: a call
to a constructor
• The name of the constructor provides the name of the class
to instantiate
• The new operator returns a reference to the object it
created. This reference is usually assigned to a variable of the
appropriate type, like:
• Point P = new Point(23, 94); 10
Blue Ridge Public School
Role of New Keyword – cont.
• Here's the code for the Point class:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
• This class contains a single constructor. You can recognize a
constructor because its declaration uses the same name as the
class and it has no return type
• The constructor in the Point class takes two integer arguments, as
declared by the code (int a, int b)
• The following statement provides 23 and 94 as values for those 11
arguments:
• Point P = new Point(23, 94);
Blue Ridge Public School
Wrapper Classes
• In Java everything is in the form of a class, except primitive
datatypes
• This is for performance reasons
• Normally, when we work with Numbers, we use primitive data
types such as byte, int, long, double, etc. For example:
• int i = 500;
• float gpa = 3.65f;
• byte mask = 0xff;
• There are, however, reasons to use objects in place of
primitives, and the Java platform provides wrapper classes for
each of the primitive data types
12
Blue Ridge Public School
Wrapper Classes – cont.
• A Wrapper class is a class whose object wraps
or contains a primitive data type
• When we create an object to a wrapper class, it
contains a field and in this field, we can store a
primitive data types
• In other words, we can wrap a primitive value
into a wrapper class object
• The need for wrapper class is that they convert
primitive data types into objects
• The wrapper classes encapsulate, or wrap the
primitive datatypes within a class 13
Blue Ridge Public School
Wrapper Classes – cont.
• The wrapper classes in java serve two primary purposes:
• Objects are needed if we wish to modify the arguments passed
into a method (because primitive types are passed by value)
14
Blue Ridge Public School
Wrapper Classes – 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.
15
Blue Ridge Public School
Wrapper Classes – cont.
• Wrapper classes form a part of standard library of java.lang
package
• This package provides many methods that help us manipulate
the primitive datatypes
• Role of wrapper class
• Determines the nature of data of primitive datatype
• Converts them to object type
• Converts object data into data of primitive type
16
Blue Ridge Public School
Wrapper Classes – cont.
• Primitive Data types and their Corresponding Wrapper class
18
Blue Ridge Public School
Wrapper Classes – cont.
• Integer Wrapper Class
• There are two ways to instantiate the Integer object
• One is to use the new keyword.
• Integer secondInteger = new Integer(100);
• And the second method to create an Integer object is to use the
autoboxing feature of java which directly converts a primitive
data type to its corresponding wrapper class
• Below is an example on how to make use of autoboxing in order
to create a new Integer object
• Integer sampleInteger = 100;
19
Blue Ridge Public School
Some of the Methods of Integer
Wrapper class
Method Return Argument Description
Type
parseInt() int String Returns int value when the function has
numbers in String format as argument
valueOf() int String Same as parseInt() but it returns Integer
object
toBinaryString() String int Returns String which is the binary
equivalent of the integer
toOctalString() String int Returns String which is the octal equivalent
of the integer in number system
parseDouble() double String Returns double value when the function has
numbers in String format as argument
valueOf() double String Same as parseDouble() but it returns
Double object
toString() String double Returns String after converting the data in
integer type passed as argument
21
Blue Ridge Public School
Some of the Methods of Float
Wrapper class
Method Return Argument Description
Type
parseFloat() float String Returns float value when the function has
numbers in String format as argument
valueOf() float String Same as parseFloat() but it returns Float
object
toString() String float Returns String after converting the data in
integer type passed as argument
22
Blue Ridge Public School
Wrapper Classes – cont.
• Character Wrapper Class
• Most of the time, if you are using a single character value, you
will use the primitive char type. For example:
• char ch = 'a';
• There are times, however, when you need to use a char as an
object—for example, as a method argument where an object is
expected
• The Java programming language provides a wrapper class that
"wraps" the char in a Character object for this purpose
• You can create a Character object with
the Character constructor:
• Character ch = new Character('a');
23
Blue Ridge Public School
Some of the Methods of Character
Wrapper class
Method Return Argument Description
Type
isLowerCase() boolean char Returns true if character passed as argument is of
lower case. Else returns false
isUpperCase() boolean char Returns true if character passed as argument is of
uppercase. Else returns false
25
Thank You!!
26