SlideShare a Scribd company logo
Blue Ridge Public School
Class X – Computer Applications
Ch 2- Library Classes
Blue
Ridge
Public
School
1
Topics
• Class as a Composite Type
Since class can consist of variables of different datatypes it is called a
composite datatype
Blue
Ridge
Public
School
2
Class as a Composite Type
• There are 8 primitive data types defined in Java, they
are byte, short, int, long, float, double, char, boolean
• Java allows us to create new data type using combination of
the primitive data types. Such data types are
called composite or user-defined data types
• Just as variables can be declared of any primitive data type,
similarly we can declare variables of the composite data
types
Blue
Ridge
Public
School
3
Class as a Composite Type – cont.
• Class is an example of composite data type and
we can declare objects as variables of the class
• A class is known as a composite data type
because it binds both member variable and
functions as a single identity
• Composite datatypes are the datatypes which
can be constructed within a program by using
primitive datatypes and other composite types
Blue
Ridge
Public
School
4
Class as a Composite Type – cont.
• Consider the following class definition:
class TypeDemo
{
byte a;
int b;
float c;
char d;
public void getData(){
.
.
}
public void display(){
.
.
}
}
You can see that primitive data types have been used for defining this class:
byte a;
int b;
float c;
char d;
Once a class is declared, variables of this class type can be declared and created e.g.,
TypeDemo obj1 = new TypeDemo();
Variables of a class type are known as objects
Blue
Ridge
Public
School
5
Defining a Class
• A class is nothing but a blueprint or a template for
creating different objects which defines its
properties and behaviours
• Java class objects exhibit the properties and
behaviours defined by its class
• A class can contain fields and methods to describe
the behaviour of an object
• Methods are nothing but members of a class that
provide a service for an object or perform some
business logic
• Methods define the operations that can be
performed in java programming
Blue
Ridge
Public
School
6
Defining a Class – cont.
• Below is an example showing the Objects and Classes of the
Cube class that defines 3 fields namely length, breadth and
height. Also the class contains a member function
getVolume()
public class Cube
{
int length;
int breadth;
int height;
public int getVolume()
{
return (length * breadth * height);
}
}
• Since class can consist of variables of different datatypes it is
called composite datatype
Blue
Ridge
Public
School
7
Object as Instance of Class
• In object-oriented programming (OOP), an instance is a
concrete occurrence of any object, existing usually during the
runtime of a computer program
• "instance" is synonymous with "object" as they are each a
particular value (realization), and these may be called
an instance object; "instance" emphasizes the distinct identity
of the object
• An object is an instance of a class, and may be called a class
instance or class object
Blue
Ridge
Public
School
8
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
Blue
Ridge
Public
School
9
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);
Blue
Ridge
Public
School
10
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
arguments:
• Point P = new Point(23, 94);
Blue
Ridge
Public
School
11
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
Blue
Ridge
Public
School
12
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
Blue
Ridge
Public
School
13
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)
• To provide an assortment 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
Blue
Ridge
Public
School
14
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.
Blue
Ridge
Public
School
15
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
Blue
Ridge
Public
School
16
Wrapper Classes – cont.
• Primitive Data types and their Corresponding Wrapper class
Blue
Ridge
Public
School
17
Primitive Datatype Wrapper Class
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
Wrapper Classes – cont.
• Integer Wrapper Class
• Integer class is a wrapper class for the primitive type int which
contains several methods to effectively deal with an int value like
converting it to a string representation, and vice-versa
• The Integer class wraps the int primitive data type into an object.
• This class includes helpful methods in converting value from
String to Integer
Blue
Ridge
Public
School
18
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;
Blue
Ridge
Public
School
19
Some of the Methods of Integer
Wrapper class
Method Return
Type
Argument Description
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
toHexString() String int Returns String which is the hexadecimal
equivalent of the integer in number system
toString() String int Returns String after converting the data in
integer type passed as argument
Blue
Ridge
Public
School
20
Wrapper Classes – cont.
• Integer Wrapper Class
• Convert Integer to Java String object
/*
Convert Integer object to String object
This example shows how a Integer object can be converted into String
object.
*/
public class IntegerToStringExample {
public static void main(String[] args) {
Integer intObj = new Integer(10);
//use toString method of Integer class to convert Integer into String.
String str = intObj.toString();
System.out.println("Integer converted to String as " + str);
}
}
/*
Output of the program would be
Integer converted to String as 10
*/
Blue
Ridge
Public
School
21
Wrapper Classes – cont.
• Integer Wrapper Class
• Convert String to int Example
/*
Convert String to int Example
This Convert String to int Java example shows how a String object can be converted
into int primitive type
using parseInt method of Integer class.
*/
public class StringToIntExample {
public static void main(String[] args) {
//declare String object
String str = "10";
/*
use parseInt method of Integer class to convert String into int primitive data type. This
is a static method.
Please note that this method can throw a NumberFormatException if the string is not
parsable to int.
*/
int i = Integer.parseInt(str);
System.out.println(i);
}
}
/*
Output the program would be
10
*/
Blue
Ridge
Public
School
22
Some of the Methods of Double
Wrapper class
Method Return
Type
Argument Description
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
Blue
Ridge
Public
School
23
Some of the Methods of Float
Wrapper class
Method Return
Type
Argument Description
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
Blue
Ridge
Public
School
24
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');
Blue
Ridge
Public
School
25
Some of the Methods of Character
Wrapper class
Method Return
Type
Argument Description
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
isDigit() boolean char Returns true if character passed as argument is
digit. Else returns false
isLetter() boolean char Returns true if character passed as argument is
letter. Else returns false
isWhitespace() boolean char Returns true if character passed as argument is
whitespace. Else returns false
isLetterOrDigit() boolean char Returns true if character passed as argument is
letter or digit. Else returns false
toUpperCase() char char Returns character after converting it to uppercase
toLowerCase() char char Returns character after converting it to lowercase
Blue
Ridge
Public
School
26
Autoboxing - Unboxing
• The automatic conversion of primitive data type into an object
of its equivalent wrapper class is known as autoboxing.
Integer wobj = new Integer(77);
• The system of converting an object of wrapper class into
primitive data type is known as unboxing.
Integer wobj = new Integer(77);
int x = wobj;
27
Blue
Ridge
Public
School
Thank You!!
Blue
Ridge
Public
School
28

More Related Content

Similar to Ch 2 Library Classes.pdf

Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DSMS Group of Institutes
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
Dennis Chang
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
Abdii Rashid
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
mcollison
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
OsamaMuhammad18
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
Abdullah Jan
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
Object oriented programming
Object oriented programmingObject oriented programming
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
Amit Gupta
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 

Similar to Ch 2 Library Classes.pdf (20)

Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
9 cm604.14
9 cm604.149 cm604.14
9 cm604.14
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
C# by Zaheer Abbas Aghani
C# by Zaheer Abbas AghaniC# by Zaheer Abbas Aghani
C# by Zaheer Abbas Aghani
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Concept of Object-Oriented in C++
Concept of Object-Oriented in C++Concept of Object-Oriented in C++
Concept of Object-Oriented in C++
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 

Recently uploaded

Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 

Recently uploaded (20)

Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 

Ch 2 Library Classes.pdf

  • 1. Blue Ridge Public School Class X – Computer Applications Ch 2- Library Classes Blue Ridge Public School 1
  • 2. Topics • Class as a Composite Type Since class can consist of variables of different datatypes it is called a composite datatype Blue Ridge Public School 2
  • 3. Class as a Composite Type • There are 8 primitive data types defined in Java, they are byte, short, int, long, float, double, char, boolean • Java allows us to create new data type using combination of the primitive data types. Such data types are called composite or user-defined data types • Just as variables can be declared of any primitive data type, similarly we can declare variables of the composite data types Blue Ridge Public School 3
  • 4. Class as a Composite Type – cont. • Class is an example of composite data type and we can declare objects as variables of the class • A class is known as a composite data type because it binds both member variable and functions as a single identity • Composite datatypes are the datatypes which can be constructed within a program by using primitive datatypes and other composite types Blue Ridge Public School 4
  • 5. Class as a Composite Type – cont. • Consider the following class definition: class TypeDemo { byte a; int b; float c; char d; public void getData(){ . . } public void display(){ . . } } You can see that primitive data types have been used for defining this class: byte a; int b; float c; char d; Once a class is declared, variables of this class type can be declared and created e.g., TypeDemo obj1 = new TypeDemo(); Variables of a class type are known as objects Blue Ridge Public School 5
  • 6. Defining a Class • A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviours • Java class objects exhibit the properties and behaviours defined by its class • A class can contain fields and methods to describe the behaviour of an object • Methods are nothing but members of a class that provide a service for an object or perform some business logic • Methods define the operations that can be performed in java programming Blue Ridge Public School 6
  • 7. Defining a Class – cont. • Below is an example showing the Objects and Classes of the Cube class that defines 3 fields namely length, breadth and height. Also the class contains a member function getVolume() public class Cube { int length; int breadth; int height; public int getVolume() { return (length * breadth * height); } } • Since class can consist of variables of different datatypes it is called composite datatype Blue Ridge Public School 7
  • 8. Object as Instance of Class • In object-oriented programming (OOP), an instance is a concrete occurrence of any object, existing usually during the runtime of a computer program • "instance" is synonymous with "object" as they are each a particular value (realization), and these may be called an instance object; "instance" emphasizes the distinct identity of the object • An object is an instance of a class, and may be called a class instance or class object Blue Ridge Public School 8
  • 9. 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 Blue Ridge Public School 9
  • 10. 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); Blue Ridge Public School 10
  • 11. 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 arguments: • Point P = new Point(23, 94); Blue Ridge Public School 11
  • 12. 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 Blue Ridge Public School 12
  • 13. 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 Blue Ridge Public School 13
  • 14. 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) • To provide an assortment 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 Blue Ridge Public School 14
  • 15. 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. Blue Ridge Public School 15
  • 16. 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 Blue Ridge Public School 16
  • 17. Wrapper Classes – cont. • Primitive Data types and their Corresponding Wrapper class Blue Ridge Public School 17 Primitive Datatype Wrapper Class char Character byte Byte short Short int Integer long Long float Float double Double boolean Boolean
  • 18. Wrapper Classes – cont. • Integer Wrapper Class • Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like converting it to a string representation, and vice-versa • The Integer class wraps the int primitive data type into an object. • This class includes helpful methods in converting value from String to Integer Blue Ridge Public School 18
  • 19. 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; Blue Ridge Public School 19
  • 20. Some of the Methods of Integer Wrapper class Method Return Type Argument Description 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 toHexString() String int Returns String which is the hexadecimal equivalent of the integer in number system toString() String int Returns String after converting the data in integer type passed as argument Blue Ridge Public School 20
  • 21. Wrapper Classes – cont. • Integer Wrapper Class • Convert Integer to Java String object /* Convert Integer object to String object This example shows how a Integer object can be converted into String object. */ public class IntegerToStringExample { public static void main(String[] args) { Integer intObj = new Integer(10); //use toString method of Integer class to convert Integer into String. String str = intObj.toString(); System.out.println("Integer converted to String as " + str); } } /* Output of the program would be Integer converted to String as 10 */ Blue Ridge Public School 21
  • 22. Wrapper Classes – cont. • Integer Wrapper Class • Convert String to int Example /* Convert String to int Example This Convert String to int Java example shows how a String object can be converted into int primitive type using parseInt method of Integer class. */ public class StringToIntExample { public static void main(String[] args) { //declare String object String str = "10"; /* use parseInt method of Integer class to convert String into int primitive data type. This is a static method. Please note that this method can throw a NumberFormatException if the string is not parsable to int. */ int i = Integer.parseInt(str); System.out.println(i); } } /* Output the program would be 10 */ Blue Ridge Public School 22
  • 23. Some of the Methods of Double Wrapper class Method Return Type Argument Description 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 Blue Ridge Public School 23
  • 24. Some of the Methods of Float Wrapper class Method Return Type Argument Description 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 Blue Ridge Public School 24
  • 25. 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'); Blue Ridge Public School 25
  • 26. Some of the Methods of Character Wrapper class Method Return Type Argument Description 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 isDigit() boolean char Returns true if character passed as argument is digit. Else returns false isLetter() boolean char Returns true if character passed as argument is letter. Else returns false isWhitespace() boolean char Returns true if character passed as argument is whitespace. Else returns false isLetterOrDigit() boolean char Returns true if character passed as argument is letter or digit. Else returns false toUpperCase() char char Returns character after converting it to uppercase toLowerCase() char char Returns character after converting it to lowercase Blue Ridge Public School 26
  • 27. Autoboxing - Unboxing • The automatic conversion of primitive data type into an object of its equivalent wrapper class is known as autoboxing. Integer wobj = new Integer(77); • The system of converting an object of wrapper class into primitive data type is known as unboxing. Integer wobj = new Integer(77); int x = wobj; 27 Blue Ridge Public School