Tutorial week-2
Identifiers, Variables, and, Operators
What we learnt in Lecture?
• Identifiers & Rules for Identifiers
• Reserved Keywords
• Variables and its types
• Operators
Variables
• Name of memory that contains a data
value.
Example of variable declaration:
int finalScore;
Example of declaration and initialization:
int finalScore = 0;
Example of declaration, then initialization:
int finalScore;
finalScore = 0;
Task
• Declare and initialize a variable of String data
type
that stores the text: “Programming”
Print the value of that variable
Operators
How can I do this
in Java?
November 18,
2022
Operators
• Certain symbols that tell the
Java compiler to perform an
operation
• The operation could range from
Operators addition of 2 numbers to
subtraction, multiplication and
so on.
November 18,
2022
Operators
• These operations are performed in operands
• Operands are the variables that are used to store the
values
Operands:
a
Operators
b
c
November 18,
2022
Types of Operators
November 18,
2022
Arithmetic Operators
• Operators used in mathematical
calculations
• Example: + , - , *, %, /
November 18,
2022
Arithmetic Operators
a b • Add the values of a and b
a b • Subtracts the values of left operand
with right operand
Left operand Right operand
November 18,
2022
Arithmetic Operators
a b • Multiplies the values of a and b
a b • Divides the numerator operand by
denominator operand
Numerator operand
Denominator operand
November 18,
2022
Arithmetic Operators
a b • Displays the remainder of the
division
a++ ++a
a-- --a
November 18,
2022
Arithmetic Operator
Examples:
System.out.println( 10 + 20); // result is 30;
System.out.println( 20 - 10); // result is 10;
System.out.println( 10 * 20); // result is 200;
System.out.println( 10 / 4); // result is 2;
System.out.println( 10 / 4.0); // result is 2.5;
System.out.println( 10 % 4); // result is 2;
System.out.println( 1 % 2); // result is 1;
System.out.println(1 / 2); // result is 0;
Relational Operators
• Used to determine the relationship that one operand has with
another
• Example: Whether two operands are equal or not equal
Whether one operand is greater than the other, and so
on.
November 18,
2022
Relational Operators
Relational
operators
November 18,
2022
Relational Operators
November 18,
2022
Relational Operator
System.out.println(10 == 20 ); //return false
System.out.println(10 != 20 ); //returns true
System.out.println(10 < 20 ); //returns true
System.out.println(10 <= 10 ); //returns true
System.out.println(10 > 20 ); //returns false
System.out.println(10 >= 5 ); //returns true
Assignment Operators
• Used to assign a new value to the variable
• Example: (=)
Assignment Operator
November 18,
2022
Conditional Operator
• Also known as ternary operator.
Ternary ?
• Ternary means 3
• This operator is applicable for 3 operands
Conditional Operator
Output:
true
Bitwise Operator
• Used to work with variables at bit level.
• Convers the values into binary format.
0 1
Types of Bitwise Operator
• 3 types of bitwise operators:
a) AND OPERATOR
b) OR OPERATOR,
c) X-OR / Exclusive OR operator
Logical Operator
• Takes Boolean values for input
• Combines 2 Boolean values and the output is also Boolean.
Logical Operators
A B &&(and) ||(or) !A
True True True True False
True False False True False
False True False True True
False False False False True
Thank You.