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

Operators in Java

The document provides an overview of operators in Java, categorizing them into unary, binary, and ternary operators. It explains the functionality of various operators including arithmetic, relational, logical, and shorthand operators, along with examples. Additionally, it discusses operator precedence, which determines the order of operations in expressions.

Uploaded by

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

Operators in Java

The document provides an overview of operators in Java, categorizing them into unary, binary, and ternary operators. It explains the functionality of various operators including arithmetic, relational, logical, and shorthand operators, along with examples. Additionally, it discusses operator precedence, which determines the order of operations in expressions.

Uploaded by

ronny47611674
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

HAL GNANAJYOTI SCHOOL

Operators in Java

Operators are symbols that perform specific operations on operands. An operator


needs one or more operands to perform any operation.

Operands are data values, which are involved in the operation. They can be
variables or constants.

Example: x*y+z

In the above example x, y and z are operands * and + are operators.

Forms of Operators in Java:


• Unary Operator
• Binary Operator and
• Ternary Operator

Unary Operators
These operators operate on only one operand.
Ex: +y, x++, --m, -r

Unary Plus (+): The Operator unary plus (+) is written before the operand. This
operator results in the same value of the variable.

Ex: if x=5 then +x means 5


if y=-10, then +y means -10

Unary Minus (˗): The Operator unary minus(-) is also written before the operand.
This operator inverts the value of an operand, i.e., if an operand is a positive value,
it converts the value of that operand to its negative value and vice versa.

Ex: If a=10, then -a means -10


If p=-2 then -p means +2
Increment (++) Operator: It increases the value stored in the variable by 1.
Decrement (--) Operator: It decreases the value stored in the variable by 1.

Prefix Increment (++x): When an increment operator precedes its operand, it is


known as prefix increment. It increases the value of operand by 1, and then
evaluates the expression. (Change and Use)

Ex: If y has a value of 10


x=++y; This will store 11 in x

Postfix Increment(x++): When an increment operator follows its operand, it is


known as postfix increment. It evaluates the expression, and then increases the
value of operand by 1. (Use and Change)

Ex: If y has a value of 10


x=y++;
x will store the value of y i.e., 10 and then y will be increased by 1.
Prefix Decrement (--x): It decreases the value of operand by 1, and then evaluates
the expression.
Ex: int y=20; x=--y; x will store the value 19

Postfix Decrement (x--): It evaluates the expression, and then decreases the value
of operand by 1.
Ex: int p=10; r=p--; r will store the value 10
the original value of p i.e., 10 will be assigned to variable r and then p will be
decreased by 1.

Types of Operators:
• Arithmetic Operator
• Relational Operator
• Logical Operator
• Assignment Operator
• Increment Decrement
• Short Hand Operators
Binary Arithmetic Operators: Operators that act upon two operands are known as
Binary Operators.

Result:
Operator Symbol Meaning Examples
If x=12 and y=4
Addition + Adds two operands x+y 16
Subtraction - Subtract right operand from the left x-y 8
Multiplication * Multiply two operands x*y 48
Division / Divide left Operand by the right one x/y 3
Remainder of the division of left
Modulus % x%y 0
operand by the right
Relational Operators: Relational Operators are used to show the relationship
between the operands. These Operators compare the values of variables and
determine the result in the boolean expression format, which means either true or
false. Java provides 6 types of relational operators.
Logical Operators in Java
Shorthand Operators/Compound Assignment Operators.

Ternary operator

The Ternary Operator (?:) is also known as a conditional operator. This operator
needs three operands to perform an operation.

Syntax: Variable=<test condition>? <Expression 1>: <Expression 2>

Here, test condition is evaluated first. If it evaluates to true then the variable
contains the value of Expression 1, otherwise, it will contain the value of
expression 2.

Example: int a=5, b=10, res;

res=(a>b)? a:b;
Precedence of Operator

Precedence of Operators means the hierarchy or order in which the operators in an


expression are evaluated when the expression has several operators.

Hierarchy (Precedence) of Operators

() []

++ -- !

* / % (Left to right whichever comes first)

+ - (Left to right whichever comes first)

< > <= >=

== !=

&&

||

?:

You might also like