SlideShare a Scribd company logo
Programming in Java
Lecture 3: Operators and Expressions
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
• Operators in Java
• Arithmetic Operators
• Bitwise Operators
• Relational Operators
• Logical Operators
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Operators in Java
• Java’s operators can be grouped into
following four categories:
1. Arithmetic
2. Bitwise
3. Relational
4. Logical.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Arithmetic Operators
• used in mathematical expressions.
• operands of the arithmetic operators must be of a
numeric type.
• most operators in Java work just like they do in
C/C++.
• We can not use them on boolean types, but we can
use them on char types, since the char type in Java is,
essentially, a subset of int.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Arithmetic Operators
Operator Result
• + Addition
• - Subtraction (also unary minus)
• * Multiplication
• / Division
• % Modulus
• ++ Increment
• += Addition assignment
• -= Subtraction assignment
• *= Multiplication assignment
• /= Division assignment
• %= Modulus assignment
• - - Decrement
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Modulus Operator (%)
• returns the remainder of a division operation.
• can be applied to floating-point types as well as integer types.
• This differs from C/C++, in which the % can only be applied to
integer types.
Example: Modulus.java
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Arithmetic Assignment Operators
• used to combine an arithmetic operation with an assignment.
Thus, the statements of the form
var = var op expression;
can be rewritten as
var op= expression;
• In Java, statements like
a = a + 5;
can be written as
a += 5;
Similarly:
b = b % 3; can be written as b %= 3;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Increment and Decrement
• ++ and the - - are Java's increment and decrement operators.
• The increment operator increases its operand by one.
• x++; is equivalent to x = x + 1;
• The decrement operator decreases its operand by one.
• y--; is equivalent to y = y – 1;
• They can appear both in
• postfix form, where they follow the operand, and
• prefix form, where they precede the operand.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• In the prefix form, the operand is incremented or
decremented before the value is obtained for use in the
expression.
Example: x = 19; y = ++x;
Output: y = 20 and x = 20
• In postfix form, the previous value is obtained for use
in the expression, and then the operand is modified.
Example: x = 19; y = x++;
Output: y = 19 and x = 20
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bitwise Operators
• These operators act upon the individual bits of their
operands.
• can be applied to the integer types, long, int, short,
char, and byte.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bitwise Logical Operators
• The bitwise logical operators are
• ~ (NOT)
• & (AND)
• | (OR)
• ^ (XOR)
• Example:
The Left Shift
• The left shift operator,<<, shifts all of the bits in
a value to the left a specified number of times.
value << num
• Example:
• 01000001 65
• << 2
• 00000100 4
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Right Shift
• The right shift operator, >>, shifts all of the bits
in a value to the right a specified number of times.
value >> num
• Example:
• 00100011 35
• >> 2
• 00001000 8
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• When we are shifting right, the top (leftmost)
bits exposed by the right shift are filled in with
the previous contents of the top bit.
• This is called sign extension and serves to
preserve the sign of negative numbers when you
shift them right.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Unsigned Right Shift
• In these cases, to shift a zero into the high-order bit no
matter what its initial value was. This is known as an
unsigned shift.
• To accomplish this, we will use Java’s unsigned, shift-right
operator, >>>, which always shifts zeros into the high-order
bit.
• Example:
• 11111111 11111111 11111111 11111111 –1 in
binary as an int
• >>>24
• 00000000 00000000 00000000 11111111 255 in
binary as an int
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Bitwise Operator Compound Assignments
• combines the assignment with the bitwise operation.
• Similar to the algebraic operators
• For example, the following two statements, which shift the
value in a right by four bits, are equivalent:
a = a >> 4;
a >>= 4;
• Likewise,
a = a | b;
a |= b;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Relational Operators
• The relational operators determine the relationship that one
operand has to the other.
• Relational operators determine equality and ordering.
• The outcome of relational operations is a boolean value.
Operator Function
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
• The result produced by a relational operator is a boolean
value.
• For example, the following code fragment is perfectly
valid:
• int a = 4;
• int b = 1;
• boolean c = a < b;
•In this case, the result of a<b (which is false) is stored in c.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Boolean Logical Operators
• The Boolean logical operators shown here operate
only on boolean operands.
Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
•All of the binary logical operators combine two
boolean values to form a resultant boolean value.
•The logical Boolean operators,&, |, and ^, operate
on boolean values in the same way that they operate
on the bits of an integer.
•The following table shows the effect of each
logical operation:
A B A | B A & B A ^ B ~ A
False False False False False True
True False True False True False
False True True False True True
True True True True False False
Short-Circuit Logical Operators
• These are secondary versions of the Boolean AND and OR operators,
and are known as short-circuit logical operators.
• OR operator results in true when A is true , no matter what B is.
• Similarly, AND operator results in false when A is false, no matter what
B is.
• If we use the || and && forms, rather than the | and & forms of these
operators, Java will not bother to evaluate the right-hand operand when the
outcome of the expression can be determined by the left operand alone.
•This is very useful when the right-hand operand depends on the value of
the left one in order to function properly.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• For example
if (denom != 0 && num / denom > 10)
• the following code fragment shows how you can take
advantage of short-circuit logical evaluation to be sure that a
division operation will be valid before evaluating it.
• circuit form of AND (&&) is used, there is no risk of
causing a run-time exception when denom is zero.
• If this line of code were written using the single & version
of AND, both sides would be evaluated, causing a run-time
exception when denom is zero.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
The Assignment Operator
• The assignment operator is the single equal sign, =.
• The assignment operator works in Java much as it does in
any other computer language.
var = expression ;
• Here, the type of var must be compatible with the type of
expression.
• The assignment operator allows to create a chain of
assignments.
• For example:
int x, y, z;
x = y = z = 100; // set x, y, and z to 100
• This fragment sets the variables x , y, and z to 100 using a
single statement.
The ? Operator
• Java includes a special ternary (three-way)operator, ?, that
can replace certain types of if-then-else statements.
•The ? has this general form:
expression1 ? expression2 : expression3
• Here,expression1 can be any expression that evaluates to a
boolean value.
• If expression1 is true , then expression2 is evaluated;
otherwise, expression3 is evaluated.
• The result of the? operation is that of the expression
evaluated.
• Both expression2 and expression3 are required to return the
same type, which can’t be void.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Example: TestTernary.java
ratio = denom == 0 ? 0 : num / denom ;
•When Java evaluates this assignment expression, it first looks
at the expression to the left of the question mark.
• If denom equals zero, then the expression between the
question mark and the colon is evaluated and used as the
value of the entire ? expression.
•If denom does not equal zero, then the expression after the
colon is evaluated and used for the value of the entire ?
expression.
•The result produced by the? operator is then assigned to ratio.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Operator Precedence
Highest
( ) [] .
++ -- %
+ -
>> >>> <<
> >= < <=
== !=
&
^
|
&&
||
?:
= Op=
Lowest
Questions

More Related Content

What's hot

Operators in java
Operators in javaOperators in java
Operators in java
Muthukumaran Subramanian
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
Marwa Ali Eissa
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
Nitin Jawla
 
java Features
java Featuresjava Features
java Features
Jadavsejal
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
Prasanna Kumar SM
 
Applets
AppletsApplets
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in JavaJin Castor
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
Ashok Raj
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
Talha Saleem
 
C tokens
C tokensC tokens
C tokens
Manu1325
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 

What's hot (20)

Operators in java
Operators in javaOperators in java
Operators in java
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
java Features
java Featuresjava Features
java Features
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Applets
AppletsApplets
Applets
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
History of java'
History of java'History of java'
History of java'
 
Manipulators in c++
Manipulators in c++Manipulators in c++
Manipulators in c++
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
C tokens
C tokensC tokens
C tokens
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 

Viewers also liked

Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in JavaAbhilash Nair
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
Javed Rashid
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressionsvishaljot_kaur
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
Atul Sehdev
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in JavaJin Castor
 
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyalarıTəhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Irkan Akhmedov
 
Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?
Irkan Akhmedov
 
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Irkan Akhmedov
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
Fernando Gil
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
OblivionWalker
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
2 java - operators
2  java - operators2  java - operators
2 java - operators
vinay arora
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesRavi_Kant_Sahu
 

Viewers also liked (20)

Operators and Expressions in Java
Operators and Expressions in JavaOperators and Expressions in Java
Operators and Expressions in Java
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
Operators and expressions
Operators and expressionsOperators and expressions
Operators and expressions
 
itft-Operators in java
itft-Operators in javaitft-Operators in java
itft-Operators in java
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Java 2
Java 2Java 2
Java 2
 
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyalarıTəhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
Təhlükəsiz proqram təminatının java mühitində hazırlanma texnologiyaları
 
Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?Fişinqdən necə qorunmaq olar?
Fişinqdən necə qorunmaq olar?
 
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
Java proqramlaşdirma mühitində təhlükəsiz proqram təminatinin hazirlanma texn...
 
Java - Operators
Java - OperatorsJava - Operators
Java - Operators
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 
Jdbc
JdbcJdbc
Jdbc
 
Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
Java arrays
Java arraysJava arrays
Java arrays
 
Array
ArrayArray
Array
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Basic IO
Basic IOBasic IO
Basic IO
 
2 java - operators
2  java - operators2  java - operators
2 java - operators
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 

Similar to Operators in java

L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
L3 operators
L3 operatorsL3 operators
L3 operators
teach4uin
 
Operators-WPS Office.pdf
Operators-WPS Office.pdfOperators-WPS Office.pdf
Operators-WPS Office.pdf
DeekshithSkandaM
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
umardanjumamaiwada
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
AyushDut
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
RithwikRanjan
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
İbrahim Kürce
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
ANANT VYAS
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
TKSanthoshRao
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Operators and expressons
Operators and expressonsOperators and expressons
Operators and expressons
Satveer Mann
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
yarkhosh
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
michaelaaron25322
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
Tushar B Kute
 
C++
C++ C++
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya College
Ramanamurthy Banda
 
Shubhrat operator &amp; expression
Shubhrat operator &amp; expressionShubhrat operator &amp; expression
Shubhrat operator &amp; expression
Shubhrat Mishra
 
Operators in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
Chëëñå Båbü
 

Similar to Operators in java (20)

L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
L3 operators
L3 operatorsL3 operators
L3 operators
 
Operators-WPS Office.pdf
Operators-WPS Office.pdfOperators-WPS Office.pdf
Operators-WPS Office.pdf
 
Oop using JAVA
Oop using JAVAOop using JAVA
Oop using JAVA
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
OPERATORS OF C++
OPERATORS OF C++OPERATORS OF C++
OPERATORS OF C++
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Operators and expressons
Operators and expressonsOperators and expressons
Operators and expressons
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
 
Python programming language introduction unit
Python programming language introduction unitPython programming language introduction unit
Python programming language introduction unit
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 
C++
C++ C++
C++
 
UNIT II_python Programming_aditya College
UNIT II_python Programming_aditya CollegeUNIT II_python Programming_aditya College
UNIT II_python Programming_aditya College
 
Shubhrat operator &amp; expression
Shubhrat operator &amp; expressionShubhrat operator &amp; expression
Shubhrat operator &amp; expression
 
Operators in java By cheena
Operators in java By cheenaOperators in java By cheena
Operators in java By cheena
 

More from Ravi_Kant_Sahu

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
Ravi_Kant_Sahu
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 

More from Ravi_Kant_Sahu (16)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Event handling
Event handlingEvent handling
Event handling
 
List classes
List classesList classes
List classes
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Packages
PackagesPackages
Packages
 
Generics
GenericsGenerics
Generics
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Inheritance
InheritanceInheritance
Inheritance
 
Jdbc
JdbcJdbc
Jdbc
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Swing api
Swing apiSwing api
Swing api
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 

Recently uploaded

Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 

Recently uploaded (20)

Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 

Operators in java

  • 1. Programming in Java Lecture 3: Operators and Expressions By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Contents • Operators in Java • Arithmetic Operators • Bitwise Operators • Relational Operators • Logical Operators Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Operators in Java • Java’s operators can be grouped into following four categories: 1. Arithmetic 2. Bitwise 3. Relational 4. Logical. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Arithmetic Operators • used in mathematical expressions. • operands of the arithmetic operators must be of a numeric type. • most operators in Java work just like they do in C/C++. • We can not use them on boolean types, but we can use them on char types, since the char type in Java is, essentially, a subset of int. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Arithmetic Operators Operator Result • + Addition • - Subtraction (also unary minus) • * Multiplication • / Division • % Modulus • ++ Increment • += Addition assignment • -= Subtraction assignment • *= Multiplication assignment • /= Division assignment • %= Modulus assignment • - - Decrement Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Modulus Operator (%) • returns the remainder of a division operation. • can be applied to floating-point types as well as integer types. • This differs from C/C++, in which the % can only be applied to integer types. Example: Modulus.java Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. Arithmetic Assignment Operators • used to combine an arithmetic operation with an assignment. Thus, the statements of the form var = var op expression; can be rewritten as var op= expression; • In Java, statements like a = a + 5; can be written as a += 5; Similarly: b = b % 3; can be written as b %= 3; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. Increment and Decrement • ++ and the - - are Java's increment and decrement operators. • The increment operator increases its operand by one. • x++; is equivalent to x = x + 1; • The decrement operator decreases its operand by one. • y--; is equivalent to y = y – 1; • They can appear both in • postfix form, where they follow the operand, and • prefix form, where they precede the operand. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. • In the prefix form, the operand is incremented or decremented before the value is obtained for use in the expression. Example: x = 19; y = ++x; Output: y = 20 and x = 20 • In postfix form, the previous value is obtained for use in the expression, and then the operand is modified. Example: x = 19; y = x++; Output: y = 19 and x = 20 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. Bitwise Operators • These operators act upon the individual bits of their operands. • can be applied to the integer types, long, int, short, char, and byte. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Operator Result ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift right >>> Shift right zero fill << Shift left &= Bitwise AND assignment |= Bitwise OR assignment ^= Bitwise exclusive OR assignment >>= Shift right assignment >>>= Shift right zero fill assignment <<= Shift left assignment Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Bitwise Logical Operators • The bitwise logical operators are • ~ (NOT) • & (AND) • | (OR) • ^ (XOR) • Example:
  • 13. The Left Shift • The left shift operator,<<, shifts all of the bits in a value to the left a specified number of times. value << num • Example: • 01000001 65 • << 2 • 00000100 4 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. The Right Shift • The right shift operator, >>, shifts all of the bits in a value to the right a specified number of times. value >> num • Example: • 00100011 35 • >> 2 • 00001000 8 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. • When we are shifting right, the top (leftmost) bits exposed by the right shift are filled in with the previous contents of the top bit. • This is called sign extension and serves to preserve the sign of negative numbers when you shift them right. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. The Unsigned Right Shift • In these cases, to shift a zero into the high-order bit no matter what its initial value was. This is known as an unsigned shift. • To accomplish this, we will use Java’s unsigned, shift-right operator, >>>, which always shifts zeros into the high-order bit. • Example: • 11111111 11111111 11111111 11111111 –1 in binary as an int • >>>24 • 00000000 00000000 00000000 11111111 255 in binary as an int Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Bitwise Operator Compound Assignments • combines the assignment with the bitwise operation. • Similar to the algebraic operators • For example, the following two statements, which shift the value in a right by four bits, are equivalent: a = a >> 4; a >>= 4; • Likewise, a = a | b; a |= b; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. Relational Operators • The relational operators determine the relationship that one operand has to the other. • Relational operators determine equality and ordering. • The outcome of relational operations is a boolean value. Operator Function == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
  • 19. • The result produced by a relational operator is a boolean value. • For example, the following code fragment is perfectly valid: • int a = 4; • int b = 1; • boolean c = a < b; •In this case, the result of a<b (which is false) is stored in c. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. Boolean Logical Operators • The Boolean logical operators shown here operate only on boolean operands. Operator Result & Logical AND | Logical OR ^ Logical XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! Logical unary NOT &= AND assignment |= OR assignment ^= XOR assignment == Equal to != Not equal to ?: Ternary if-then-else
  • 21. •All of the binary logical operators combine two boolean values to form a resultant boolean value. •The logical Boolean operators,&, |, and ^, operate on boolean values in the same way that they operate on the bits of an integer. •The following table shows the effect of each logical operation: A B A | B A & B A ^ B ~ A False False False False False True True False True False True False False True True False True True True True True True False False
  • 22. Short-Circuit Logical Operators • These are secondary versions of the Boolean AND and OR operators, and are known as short-circuit logical operators. • OR operator results in true when A is true , no matter what B is. • Similarly, AND operator results in false when A is false, no matter what B is. • If we use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand when the outcome of the expression can be determined by the left operand alone. •This is very useful when the right-hand operand depends on the value of the left one in order to function properly. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. • For example if (denom != 0 && num / denom > 10) • the following code fragment shows how you can take advantage of short-circuit logical evaluation to be sure that a division operation will be valid before evaluating it. • circuit form of AND (&&) is used, there is no risk of causing a run-time exception when denom is zero. • If this line of code were written using the single & version of AND, both sides would be evaluated, causing a run-time exception when denom is zero. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. The Assignment Operator • The assignment operator is the single equal sign, =. • The assignment operator works in Java much as it does in any other computer language. var = expression ; • Here, the type of var must be compatible with the type of expression. • The assignment operator allows to create a chain of assignments. • For example: int x, y, z; x = y = z = 100; // set x, y, and z to 100 • This fragment sets the variables x , y, and z to 100 using a single statement.
  • 25. The ? Operator • Java includes a special ternary (three-way)operator, ?, that can replace certain types of if-then-else statements. •The ? has this general form: expression1 ? expression2 : expression3 • Here,expression1 can be any expression that evaluates to a boolean value. • If expression1 is true , then expression2 is evaluated; otherwise, expression3 is evaluated. • The result of the? operation is that of the expression evaluated. • Both expression2 and expression3 are required to return the same type, which can’t be void. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. • Example: TestTernary.java ratio = denom == 0 ? 0 : num / denom ; •When Java evaluates this assignment expression, it first looks at the expression to the left of the question mark. • If denom equals zero, then the expression between the question mark and the colon is evaluated and used as the value of the entire ? expression. •If denom does not equal zero, then the expression after the colon is evaluated and used for the value of the entire ? expression. •The result produced by the? operator is then assigned to ratio. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27. Operator Precedence Highest ( ) [] . ++ -- % + - >> >>> << > >= < <= == != & ^ | && || ?: = Op= Lowest