SlideShare a Scribd company logo
WEEK 2
Instructor:
Nazish Basir
OBJECT
ORIENTED
PROGRAMMIN
G
TOPICS
• T H E S C O P E A N D L I F E T I M E O F
VA R I A B L E S
• T Y P E C O N V E R S I O N I N J AVA
• W I D E N I N G O R AU TO M AT I C T Y P E
C O N V E R S I O N
• N A R R OW I N G O R E X P L I C I T C O N V E R S I O N
• T Y P E P R O M OT I O N I N E X P R E S S I O N S
• E X P L I C I T T Y P E C A S T I N G I N
E X P R E S S I O N S
• O N E D I M E N S I O N A L A R R AYS ( 1 D A R R AY )
• T WO – D I M E N S I O N A L A R R AY ( 2 D -
THE SCOPE AND LIFETIME OF VARIABLES
• Every time you start a new block, you are creating a new scope.
• A scope determines what objects are visible to other parts of
your program.
• It also determines the lifetime of those objects.
• Scopes can be nested.
• The objects declared in the outer scope will be visible to code
within the inner scope.
• Objects declared within the inner scope will not be visible
outside it.
TYPE CONVERSION IN JAVA
• When you assign value of one data type to another
if the data types are compatible, then Java will
perform the conversion automatically known as
AutomaticType Conversion.
• If not then they need to be casted or converted
explicitly.
AUTOMATIC TYPE CONVERSION
• Also known as ImplicitType Conversion OR
Widening Conversion
• This happens when:
–The two data types are compatible.
–When we assign value of a smaller data type to a bigger
data type.
VALID DATA TYPES FOR ASSIGNMENT
DataType Compatible DataTypes
byte byte
short byte, short
int byte, short, int, char
long byte, short, int, char, long
float byte, short, int, char, long, float
double byte, short, int, char, long, float, double
boolean boolean
char char
NARROWING OR EXPLICIT CONVERSION
• If we want to assign a value of larger data type to a
smaller data type we perform explicit type casting
or narrowing.
• To create a conversion between two incompatible
types, you must use a cast.
• A cast is simply an explicit type conversion. It has
this general form:
(target-type) value
TYPE PROMOTION IN EXPRESSIONS
• Some conditions for type promotion are:
–Java automatically promotes each byte, short, or char
operand to int when evaluating an expression.
–If one operand is a long, float or double the whole
expression is promoted to long, float or double
respectively.
EXPLICIT TYPE CASTING IN EXPRESSIONS
• If we store that result in any smaller data type it
generates compile time error, due to which we need to
type cast the result.
• For example:
byte b = 50;
b = b * 2; //Error! Can’t assign int to byte
• You should use an explicit cast, such as:
b = (byte)(b * 2);
EXERCISE 1:
Write down a program which has 4 int
variables a, b, c and d with different values.
Then store the result of equation
“ab + cd”
in a variable “ans” of float data type.
The result should be accurate.
ARRAYS
• Java provides a data structure, the array, which stores a fixed-
size sequential collection of elements of the same type.
• An array is used to store a collection of data, but it is often
more useful to think of an array as a collection of variables of
the same type.
• Instead of declaring individual variables, such as:
number0, number1, ..., and number99,
• You declare one array variable such as numbers to represent
individual variables:
numbers[0], numbers[1], and ..., numbers[99]
DECLARING ARRAY VARIABLES
• Here is the syntax for declaring an array variable:
dataType[] arrayRefVar;
or
dataType arrayRefVar[];
• You can declare multiple arrays of the same data type in
one statement by inserting a comma after each array
name, using this syntax:
dataType[] arrayName1, arrayName2;
CREATING ARRAYS
• You can create an array by using the new operator with the
following syntax:
arrayRefVar = new dataType[arraySize];
• It assigns the reference of the newly created array to the
variable arrayRefVar.
• Declaring an array variable, creating an array, and assigning the
reference of the array to the variable can be combined in one
statement, as shown below:
dataType[] arrayRefVar = new
dataType[arraySize];
• For Example: int[] arr = new int[10];
CREATING ARRAYS
• Alternatively you can create arrays as follows:
dataType[] arrayRefVar = {value0, value1, ...,
valuek};
• The number of elements in the array is determined by the number
of values in the initialization list.
• The values can be an expression, for example, nine and nine + 2.
int nine = 9;
int[] oddNumbers = {1 ,3 ,5 ,7 ,nine ,nine+2, 13,};
ACCESSING ARRAY ELEMENTS
• Elements of an array are accessed using index, within the array.
• The index of the first element in the array is always 0 and the
index of the last element is always 1 less than the number of
elements.
• Arrays have a read-only, integer instance variable, length, which
holds the number of elements in the array.
• To access the number of elements in an array named
arrayName, use this syntax:
arr.length
• Thus, to access the last element of an array, use this syntax:
arr [arr.length – 1 ]
EXERCISE 2:
Write down a program which has an array of
10 integer numbers then write the code to
find the largest and smallest number from that
array
TWO – DIMENSIONAL ARRAY (2D-ARRAY)
• A two – dimensional array can be seen as an array of one –
dimensional array for easier understanding.
• Syntax to Declare Multidimensional Array in Java:
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
• Example to instantiate Multidimensional Array in Java
int[][] arr=new int[3][3];//3 row and 3 column
TWO – DIMENSIONAL ARRAY (2D-ARRAY)
• When you allocate memory for a multidimensional array, you
need only specify the memory for the first (leftmost) dimension.
• You can allocate the remaining dimensions separately.
int twoD[][] = new int[3][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
• You do not need to allocate the same number of elements for
each dimension.
• You can create a two-dimensional array in which the sizes of the
second dimension are unequal.
JAVA STRING
• String, is not a simple type.
• Nor is it simply an array of characters.
• Rather, String defines an object.
• The String type is used to declare string variables.
• You can also declare arrays of strings.
• String constant can be assigned to a String variable.
• A variable of type String can be assigned to another variable of type
String.
• You can use an object of type String as an argument to println( ).
String str = "this is a test";
System.out.println(str);
END

More Related Content

Similar to Object oriented programming2 Week 2.pptx (20)

PPT
Arrays Basics
Nikhil Pandit
 
PDF
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
PPTX
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
DOCX
Data structure and algorithm.
Abdul salam
 
PPTX
Introduction To Programming In R for data analyst
ssuser26ff68
 
PDF
java.pdf
RAJCHATTERJEE24
 
PPT
Array
PRN USM
 
PPTX
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
PPT
Core java concepts
Chikugehlot
 
PPT
Data structure
Muhammad Farhan
 
PPTX
Introduction to java
rishi ram khanal
 
PPTX
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
PPTX
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
teddiyfentaw
 
PPTX
Pandas csv
Devashish Kumar
 
PDF
advancedvlsisystemverilogbychinnifinal-201114060759.pdf
narayanakiran2
 
PPTX
Introduction to System verilog
Pushpa Yakkala
 
PPTX
Array lecture
Joan Saño
 
PPT
Data structures cs301 power point slides lecture 01
shaziabibi5
 
DOCX
Array andfunction
Girmachew Tilahun
 
Arrays Basics
Nikhil Pandit
 
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Object oriented programming1 Week 1.pptx
MirHazarKhan1
 
Data structure and algorithm.
Abdul salam
 
Introduction To Programming In R for data analyst
ssuser26ff68
 
java.pdf
RAJCHATTERJEE24
 
Array
PRN USM
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
aniketraj4440
 
Core java concepts
Chikugehlot
 
Data structure
Muhammad Farhan
 
Introduction to java
rishi ram khanal
 
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
wrushabhsirsat
 
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
teddiyfentaw
 
Pandas csv
Devashish Kumar
 
advancedvlsisystemverilogbychinnifinal-201114060759.pdf
narayanakiran2
 
Introduction to System verilog
Pushpa Yakkala
 
Array lecture
Joan Saño
 
Data structures cs301 power point slides lecture 01
shaziabibi5
 
Array andfunction
Girmachew Tilahun
 

Recently uploaded (20)

PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Thermal runway and thermal stability.pptx
godow93766
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Ad

Object oriented programming2 Week 2.pptx

  • 2. TOPICS • T H E S C O P E A N D L I F E T I M E O F VA R I A B L E S • T Y P E C O N V E R S I O N I N J AVA • W I D E N I N G O R AU TO M AT I C T Y P E C O N V E R S I O N • N A R R OW I N G O R E X P L I C I T C O N V E R S I O N • T Y P E P R O M OT I O N I N E X P R E S S I O N S • E X P L I C I T T Y P E C A S T I N G I N E X P R E S S I O N S • O N E D I M E N S I O N A L A R R AYS ( 1 D A R R AY ) • T WO – D I M E N S I O N A L A R R AY ( 2 D -
  • 3. THE SCOPE AND LIFETIME OF VARIABLES • Every time you start a new block, you are creating a new scope. • A scope determines what objects are visible to other parts of your program. • It also determines the lifetime of those objects. • Scopes can be nested. • The objects declared in the outer scope will be visible to code within the inner scope. • Objects declared within the inner scope will not be visible outside it.
  • 4. TYPE CONVERSION IN JAVA • When you assign value of one data type to another if the data types are compatible, then Java will perform the conversion automatically known as AutomaticType Conversion. • If not then they need to be casted or converted explicitly.
  • 5. AUTOMATIC TYPE CONVERSION • Also known as ImplicitType Conversion OR Widening Conversion • This happens when: –The two data types are compatible. –When we assign value of a smaller data type to a bigger data type.
  • 6. VALID DATA TYPES FOR ASSIGNMENT DataType Compatible DataTypes byte byte short byte, short int byte, short, int, char long byte, short, int, char, long float byte, short, int, char, long, float double byte, short, int, char, long, float, double boolean boolean char char
  • 7. NARROWING OR EXPLICIT CONVERSION • If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing. • To create a conversion between two incompatible types, you must use a cast. • A cast is simply an explicit type conversion. It has this general form: (target-type) value
  • 8. TYPE PROMOTION IN EXPRESSIONS • Some conditions for type promotion are: –Java automatically promotes each byte, short, or char operand to int when evaluating an expression. –If one operand is a long, float or double the whole expression is promoted to long, float or double respectively.
  • 9. EXPLICIT TYPE CASTING IN EXPRESSIONS • If we store that result in any smaller data type it generates compile time error, due to which we need to type cast the result. • For example: byte b = 50; b = b * 2; //Error! Can’t assign int to byte • You should use an explicit cast, such as: b = (byte)(b * 2);
  • 10. EXERCISE 1: Write down a program which has 4 int variables a, b, c and d with different values. Then store the result of equation “ab + cd” in a variable “ans” of float data type. The result should be accurate.
  • 11. ARRAYS • Java provides a data structure, the array, which stores a fixed- size sequential collection of elements of the same type. • An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. • Instead of declaring individual variables, such as: number0, number1, ..., and number99, • You declare one array variable such as numbers to represent individual variables: numbers[0], numbers[1], and ..., numbers[99]
  • 12. DECLARING ARRAY VARIABLES • Here is the syntax for declaring an array variable: dataType[] arrayRefVar; or dataType arrayRefVar[]; • You can declare multiple arrays of the same data type in one statement by inserting a comma after each array name, using this syntax: dataType[] arrayName1, arrayName2;
  • 13. CREATING ARRAYS • You can create an array by using the new operator with the following syntax: arrayRefVar = new dataType[arraySize]; • It assigns the reference of the newly created array to the variable arrayRefVar. • Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be combined in one statement, as shown below: dataType[] arrayRefVar = new dataType[arraySize]; • For Example: int[] arr = new int[10];
  • 14. CREATING ARRAYS • Alternatively you can create arrays as follows: dataType[] arrayRefVar = {value0, value1, ..., valuek}; • The number of elements in the array is determined by the number of values in the initialization list. • The values can be an expression, for example, nine and nine + 2. int nine = 9; int[] oddNumbers = {1 ,3 ,5 ,7 ,nine ,nine+2, 13,};
  • 15. ACCESSING ARRAY ELEMENTS • Elements of an array are accessed using index, within the array. • The index of the first element in the array is always 0 and the index of the last element is always 1 less than the number of elements. • Arrays have a read-only, integer instance variable, length, which holds the number of elements in the array. • To access the number of elements in an array named arrayName, use this syntax: arr.length • Thus, to access the last element of an array, use this syntax: arr [arr.length – 1 ]
  • 16. EXERCISE 2: Write down a program which has an array of 10 integer numbers then write the code to find the largest and smallest number from that array
  • 17. TWO – DIMENSIONAL ARRAY (2D-ARRAY) • A two – dimensional array can be seen as an array of one – dimensional array for easier understanding. • Syntax to Declare Multidimensional Array in Java: dataType[][] arrayRefVar; (or) dataType [][]arrayRefVar; (or) dataType arrayRefVar[][]; (or) dataType []arrayRefVar[]; • Example to instantiate Multidimensional Array in Java int[][] arr=new int[3][3];//3 row and 3 column
  • 18. TWO – DIMENSIONAL ARRAY (2D-ARRAY) • When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. • You can allocate the remaining dimensions separately. int twoD[][] = new int[3][]; twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; • You do not need to allocate the same number of elements for each dimension. • You can create a two-dimensional array in which the sizes of the second dimension are unequal.
  • 19. JAVA STRING • String, is not a simple type. • Nor is it simply an array of characters. • Rather, String defines an object. • The String type is used to declare string variables. • You can also declare arrays of strings. • String constant can be assigned to a String variable. • A variable of type String can be assigned to another variable of type String. • You can use an object of type String as an argument to println( ). String str = "this is a test"; System.out.println(str);
  • 20. END