0% found this document useful (0 votes)
33 views12 pages

Java Operators: Types and Usage Guide

This document provides an overview of operators in Java, including their types such as arithmetic, assignment, comparison, logical, unary, bitwise, shift, and ternary operators. It explains how to use these operators for various calculations and comparisons, highlighting their functionality and syntax. Additionally, it details the purpose and usage of specific operators, including examples of arithmetic and assignment operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Java Operators: Types and Usage Guide

This document provides an overview of operators in Java, including their types such as arithmetic, assignment, comparison, logical, unary, bitwise, shift, and ternary operators. It explains how to use these operators for various calculations and comparisons, highlighting their functionality and syntax. Additionally, it details the purpose and usage of specific operators, including examples of arithmetic and assignment operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Objectives

In this session, you will learn to:


Work with operators
Use operator precedence
Working with Operators
Scenario:

The application
should validate
Needs to calculate the user input.
the number of
attempts in which
a user gives the
correct answer for The application
should
a jumbled word.
increment the
counter by one
on each
attempt the
user provides
an incorrect
Sam input.
Working with Operators (Contd.)
Scenario (Contd.):

For such calculations and


comparisons, Java provides various
types of operators.
Working with Operators (Contd.)
An operator is a special symbol that is combined with one or more
operands.
The following figure shows the operator and operands.
Working with Operators (Contd.)
The following types of operators are supported in Java:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Unary operators
Bitwise operators
Shift operators
Ternary operator
Using the Arithmetic Operators
Arithmetic operators are used to perform arithmetic operation on
operands.
Types of arithmetic operators:

Multiply Modulus
Plus (+) Minus (-) Divide (/)
(*) (%)
• Used to • Used to • Used to • Used to • Used to
add two subtract multiply divide find the
numbers. two two one remainde
numbers. numbers. number r after
by dividing
another. the two
numbers.
Using the Assignment Operators
The assignment operators can be categorized into:

Simple assignment operator

Complex assignment operator

Simple assignment operator, =, is used to assign a value to a


variable.
Complex assignment operators are:
Also known as arithmetic assignment operators.
Shorthand for their equivalent long forms.
Using the Assignment Operators (Contd.)
Types of complex assignment operators:
• Used to add two numbers and assign the result
+ to a variable.
=
• Used to subtract two numbers and assign the
- result to a variable.
=
• Used to multiply two numbers and assign the
* result to a variable.
=
• Used to divide one number by another and
/ assign the result to a variable.
=
• Used to find the remainder and assign the result
% to a variable.
=
Using the Comparison Operators
Comparison operators are used to:
Compare two values.
Perform an action on the basis of the result of that comparison.
Whenever you use a comparison operator, the expression results in
the boolean value, true or false.
Using the Comparison Operators (Contd.)
Types of comparison operators:

Greater
Less than
Less than Greater than or Equal to Not equal
or equal
(<) than (>) equal to (==) to (!=)
to (<=)
(>=)

Used to Used to
Used to
Used to check check Used to Used to
check
check whether whether check check
whether
whether the value of the value of whether whether
the value of
the value of the left the left the value of the value of
the left
the left operand is operand is the left the left
operand is
operand is less than or greater operand is operand is
greater
less than equal to the than or equal to the not equal to
than the
the value of value of the equal to the value of the the value of
value of the
the right right value of the right the right
right
operand. right operand. operand.
operand. operand. operand.
Using the Comparison Operators (Contd.)
There is one more comparison operator called the instanceof
operator, which is used to test whether an object is an instance of a
specific class at runtime or not.
The syntax of the instanceof operator is:
op1 instanceof op2
Using the Logical Operators
Logical operators are used to evaluate operands and return a
boolean value.
Types of logical operators:

Logical Used to
Logical
AND(&& compare two
OR (||) boolean
) Used to
expressions
compare two
and returns
boolean
false if both
expressions
the boolean
and returns
expressions
true if both
are false, else
the boolean
returns true if
expressions
any one of the
are true.
boolean
expression is
true.

You might also like