0% found this document useful (0 votes)
21 views

Operators

Uploaded by

VIJAY ANAND
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Operators

Uploaded by

VIJAY ANAND
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

P3 JAVA

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

Relational comparison < > <= >= instanceof

equality == !=

Shift shift << >> >>>

Java Left Shift Operator Example


public class OperatorExample{
public static void main(String args[]){
System.out.println(10<<2);//10*2^2=10*4=40
System.out.println(10<<3);//10*2^3=10*8=80
}
}
The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified number
of times.
The Java right shift operator >> is used to move the value of the left operand to right by the number of bits
specified by the right operand.
System.out.println(10>>2);//10/2^2=10/4=2 (Right Shift) 12 June 2023 16
Logical and BitwiseOperator,

Logical logical AND &&


logical OR ||

Bitwise bitwise AND &


bitwise exclusive OR ^
bitwise inclusive OR |

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,

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);//false && true = false
System.out.println(a);//10 because second condition is not checked
System.out.println(a<b&a++<c);//false && true = false
System.out.println(a);//11 because second condition is checked
}}

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

You might also like