Lecture No 4 (OOP)
Lecture No 4 (OOP)
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
Boolea
Byte Short Int Long Float Double Char
n
1 2B 4 8 4 8 2 1 bit
B B B B B B
Operators in Arithmetic
operators
Assignment
operators
Java
Comparison
Logical operators
operators
Arithmetic
operators are used
Arithmetic Operators
to perform simple
mathematical
operations.
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.