Operators
Operators
12 June 2023 1
Module-1
Overview of Java: Programming with Eclipse IDE, Operators, Control Flow, and Coding
Exercises for Operators using decision making statements.
12 June 2023 2
Introduction
12 June 2023 3
Java Platform
12 June 2023 4
12 June 2023 5
A First Example
12 June 2023 6
Data Types
12 June 2023 7
Variables
12 June 2023 8
Constants
12 June 2023 9
operators
⚫ Arithmetic operators
⚫ Assignment operators
⚫ Comparison operators
⚫ Logical operators
⚫ Bitwise operators
12 June 2023 10
Arithmetic Operators
12 June 2023 11
12 June 2023 12
12 June 2023 13
Assignment Operators
Java assignment operator is one of the most common operators. It is used to assign the value on its
right to the operand on its left.
⚫ public class OperatorExample{
⚫ public static void main(String[] args){
⚫ int a=10;
⚫ a+=3;//10+3
⚫ System.out.println(a);
⚫ a-=4;//13-4
⚫ System.out.println(a);
⚫ a*=2;//9*2
⚫ System.out.println(a);
⚫ a/=2;//18/2
⚫ System.out.println(a);
⚫ }}
12 June 2023 14
12 June 2023 15
Relational and Shift Operator
equality == !=
The logical && operator doesn't check the second condition if the first condition is false. It checks the
second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
12 June 2023 17
Logical and BitwiseOperator,
False
10
False
11
12 June 2023 18
Logical and BitwiseOperator,
The logical || operator doesn't check the second condition if the first condition is true. It checks the second
condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
int c=20;
System.out.println(a>b||a<c);//true || true = true
System.out.println(a>b|a<c);//true | true = true
//|| vs |
System.out.println(a>b||a++<c);//true || true = true
System.out.println(a);//10 because second condition is not checked
System.out.println(a>b|a++<c);//true | true = true
System.out.println(a);//11 because second condition is checked
}}
12 June 2023 19
Java Ternary Operator
Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in Java
programming. It is the only conditional operator which takes three operands.
public class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}}
12 June 2023 20
Java User Input
The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class
documentation. In our example, we will use the nextLine() method, which is used to read Strings:
12 June 2023 21