0% found this document useful (0 votes)
2 views13 pages

Lecture No 4 (OOP)

The document provides an overview of Object Oriented Programming concepts in Java, focusing on constants, primitive and non-primitive data types, and various operators. It explains the use of the final keyword for creating constants and preventing inheritance, as well as detailing the eight primitive data types and their value ranges. Additionally, it covers different types of operators including arithmetic, assignment, comparison, and logical operators.

Uploaded by

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

Lecture No 4 (OOP)

The document provides an overview of Object Oriented Programming concepts in Java, focusing on constants, primitive and non-primitive data types, and various operators. It explains the use of the final keyword for creating constants and preventing inheritance, as well as detailing the eight primitive data types and their value ranges. Additionally, it covers different types of operators including arithmetic, assignment, comparison, and logical operators.

Uploaded by

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

Object Oriented

Programming
By Samra Tariq (Gold medalist), lecturer at NUML
Theoretical Concepts

D F N T O
E I I I N

C O N C E P T S
Constants in
java (final
keyword)
Tip
In Java, a constant is a variable When declaring
whose value cannot be constants, it's good
changed once it's assigned. practice to use all
uppercase letters for
the constant name.
Why constants? This makes it easier to
Constants are often used to represent values that will not identify constants in
change, such as the number of students in a class. They can code. Like PI,
also be used to make the code more readable by giving MY_CONSTANT, etc.
names to values that would otherwise be hard to remember.
Where to declare constants?
A constant can be declared within a method, class, or
interface.
How to declare constants?
To declare a constant in a class, we use the final keyword
such as:
final float PI = 3.1416;
Pros of
constant Pros of declaring
variables in constant variables in
java
Code

1. To make code more readable by giving names


to values
2. To prevent a variable from being changed
accidentally
3. To enforce the immutability of an object
Other uses of
final keyword Final keyword

1. Final keyword in java is used to create


constants
2. Final keyword is used to prevent inheritance
3. And it is used to prevent methods from being
overridden.
Primitive & non-
primitive data Data type
types in java
Non-
Primitive
primitive

There are eight primitive data Non-primitive types


types in java. are created by the
programmer and is
not defined by Java
(except for String).

Boolea
Byte Short Int Long Float Double Char
n

1 2B 4 8 4 8 2 1 bit
B B B B B B

Arrays Classes String Interface


Primitive & non-
primitive data
types in java
Value Ranges of Primitive
data types

Data type Description


Byte Stores whole numbers from -128 to 127
Short Stores whole numbers from -32,768 to 32,767
Int Stores whole numbers from -2,147,483,648 to 2,147,483,647
Long Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Float Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits.
Double Stores fractional numbers. Sufficient for storing 15 decimal digits.
Char Stores a single character/letter or ASCII values.
Boolean Stores true or false values.
++
+= + /
>= && - %
Operators in java
Operators and their types
Operators

Operators in Arithmetic
operators
Assignment
operators
Java

Comparison
Logical operators
operators
Arithmetic
operators are used
Arithmetic Operators
to perform simple
mathematical
operations.

1. Arithmetic Unary Operators Binary operators

Operators

Operators that work Operators that work


with single operand with two operands
e.g., ++, --, +, - e.g., +, -, *, /, and %
Like a++, a--, +a, -a Like a + b, a % b
Assignment
operators are used Assignment Operators
to assign values to
variables. In java
programming (=)
sign is used to
assign values to
variables.
2. Assignment Compound assignment
Operators
Operators

The compound assignment operators consist of


a binary operator and a simple assignment
operator. They perform the operation of the
binary operator on both operands and store the
result of that operation in the left operand. E.g.,
+=, -=, *=, /=, and %=
Compariso
n
Operators

Comparison
operators are used == !=
to compare two
3. Comparison values (or
variables). This is
Operators important in > <
programming
because it helps us
to find answers and
make decisions. The >= >=
return value of a
comparison is either
true or false.
Logical operators
are used to Logical
determine the logic Operators
between variables
or values.

4. Logical Logical AND


Returns true if both && ||
Operators statements
true.
are

Logical NOT Logical OR


Reverse the result, ! Returns true if one
returns false if the of the statements is
result is true. true

You might also like