0% found this document useful (0 votes)
7 views13 pages

Java Operators Explained: Types & Examples

The document provides an overview of operators in Java, detailing various types such as unary, arithmetic, relational, bitwise, logical, ternary, and assignment operators. It includes examples of how these operators function, their precedence, and specific use cases in Java programming. Additionally, it explains the differences between logical and bitwise operators, as well as the ternary and assignment operators with practical code snippets.

Uploaded by

Naga Bhushan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Java Operators Explained: Types & Examples

The document provides an overview of operators in Java, detailing various types such as unary, arithmetic, relational, bitwise, logical, ternary, and assignment operators. It includes examples of how these operators function, their precedence, and specific use cases in Java programming. Additionally, it explains the differences between logical and bitwise operators, as well as the ternary and assignment operators with practical code snippets.

Uploaded by

Naga Bhushan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Operators in Java

Operator in Java is a symbol that is used to


perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which
are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Java Operator Precedence

Operator Category Precedence


Type

Unary postfix expr++ expr--

prefix ++expr --expr


+expr -expr ~ !
Arithmetic multiplicativ * / %
e
additive + -
Shift shift << >> >>>
Relational comparison < > <= >=
instanceof
equality == !=
Bitwise bitwise AND &

bitwise ^
exclusive OR
bitwise |
inclusive OR
Logical logical AND &&

logical OR ||
Ternary ternary ? :
Assignmen assignment = += -= *= /= %= &=
t ^= |= <<= >>= >>>=

Java Unary Operator


The Java unary operators require only one
operand. Unary operators are used to perform
various operations i.e.:
o incrementing/decrementing a value by one
o negating an expression
o inverting the value of a boolean
Java Unary Operator Example: ++ and --
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] x=10;
[Link](x++);//10 (11)
[Link](++x);//12
[Link](x--);//12 (11)
[Link](--x);//10
8.}}
Output:
10
12
12
10

Java Unary Operator Example 2: ++ and --


[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=10;
[Link](a++ + ++a);//10+12=22
[Link](b++ + b++);//10+11=21
7.
8.}}
Output:
22
21
Java Unary Operator Example: ~ and !
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=-10;
[Link] c=true;
[Link] d=false;
[Link](~a);
//-11 (for any n value ~(n)=-(n+1))
[Link](~b);//9
[Link](!c);//false (opposite of boo
lean value)
10. [Link](!d);//true
11. }}
Output:
-11
9
false
true

Java Arithmetic Operators


Java arithmetic operators are used to perform
addition, subtraction, multiplication, and division.
They act as basic mathematical operations.
Java Arithmetic Operator Example
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=5;
[Link](a+b);//15
[Link](a-b);//5
[Link](a*b);//50
[Link](a/b);//2
[Link](a%b);//0
10. }}
Output:
15
5
50
2
0

Java Arithmetic Operator Example: Expression


[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link](10*10/5+3-1*4/2);
4.}}
Output:

21

Types of Relational Operators in Java


Types of Relational Operators
The commonly used relational operators in Java are:
Equal to (==): Checks if two values are equal.
Not equal to (!=): Verifies if two values are not equal.
Greater than (>): Tests if the left operand is greater than
the right operand.
Less than (<): Determines if the left operand is less than
the right operand.
Greater than or equal to (>=): Check if the left operand
is greater than or equal to the right operand.
Less than or equal to (<=): Verifies if the left operand is
less than or equal to the right operand.
Java Left Shift Operator
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.
Java Left Shift Operator Example
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link](10<<2);//
10*2^2=10*4=40
[Link](10<<3);//
10*2^3=10*8=80
[Link](20<<2);//
20*2^2=20*4=80
[Link](15<<4);//
15*2^4=15*16=240
7.}}
Output:
40
80
80
240

Java Right Shift Operator


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.
Java Right Shift Operator Example
[Link] OperatorExample{
[Link] static void main(String args[]){
[Link](10>>2);//10/2^2=10/4=2

[Link](20>>2);//20/2^2=20/4=5

[Link](20>>3);//20/2^3=20/8=2

6.}}
Output:
2
5
2

Java AND Operator Example: Logical && and Bitwise &


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.
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=5;
[Link] c=20;
[Link](a<b&&a<c);//false && tru
e = false
[Link](a<b&a<c);//false & true =
false
8.}}
Output:
false
false

Java AND Operator Example: Logical && vs Bitwise &


[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=5;
[Link] c=20;
[Link](a<b&&a++<c);//false &&
true = false
[Link](a);//10 because second co
ndition is not checked
[Link](a<b&a++<c);//false && tr
ue = false
[Link](a);//11 because second co
ndition is checked
10. }}
Output:
false
10
false
11

Java OR Operator Example: Logical || and Bitwise |


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.
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=5;
[Link] c=20;
[Link](a>b||a<c);//true || true =
true
[Link](a>b|a<c);//true | true = tr
ue
8.//|| vs |
[Link](a>b||a++<c);//true || true
= true
10. [Link](a);//10 because secon
d condition is not checked
11. [Link](a>b|a++<c);//true | tr
ue = true
12. [Link](a);//11 because secon
d condition is checked
13. }}
Output:
true
true
true
10
true
11

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.
Java Ternary Operator Example
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=2;
[Link] b=5;
[Link] min=(a<b)?a:b;
[Link](min);
7.}}
Output:
2

Another Example:
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=5;
[Link] min=(a<b)?a:b;
[Link](min);
7.}}
Output:
5

Java Assignment Operator


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.
Java Assignment Operator Example
[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=20;
5.a+=4;//a=a+4 (a=10+4)
6.b-=4;//b=b-4 (b=20-4)
[Link](a);
[Link](b);
9.}}
Output:
14
16

Java Assignment Operator Example


[Link] class OperatorExample{
[Link] static void main(String[] args){
[Link] a=10;
4.a+=3;//10+3
[Link](a);
6.a-=4;//13-4
[Link](a);
8.a*=2;//9*2
[Link](a);
10. a/=2;//18/2
11. [Link](a);
12. }}
Output:
13
9
18
9

Java Assignment Operator Example: Adding short


[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=10;
5.//a+=b;//a=a+b internally so fine
6.a=a+b;//Compile time error because 10+10=
20 now int
[Link](a);
8.}}
Output:
Compile time error

After type cast:


[Link] class OperatorExample{
[Link] static void main(String args[]){
[Link] a=10;
[Link] b=10;
5.a=(short)(a+b);//20 which is int now convert
ed to short
[Link](a);
7.}}
Output:
20

Common questions

Powered by AI

Shift operators in Java manipulate the bits of a variable’s binary representation. The left shift operator (<<) shifts bits to the left, effectively multiplying the number by 2 to the power of the number of positions shifted. For example, '10<<2' results in 40. The right shift operator (>>) shifts bits to the right, dividing the number by 2 raised to the power of the number of positions shifted. '20>>2' results in 5 .

The assignment operator in Java assigns the value on the right side of the operator to the variable on the left side. When dealing with different data types, particularly primitives, implicit type conversion may occur. An explicit type cast is required when assigning a larger data type to a smaller one. For instance, 'short a = 10; short b = 10; a = (short) (a+b);' is necessary to avoid a compilation error when the result of 'a + b' (automatically an int) is assigned back to a short .

The ternary operator in Java acts as a succinct alternative to if-then-else statements, using three operands. The syntax is: 'condition ? expressionIfTrue : expressionIfFalse'. For example, 'int min = (a < b) ? a : b;' assigns the value of 'a' to 'min' if 'a' is less than 'b', otherwise 'b' .

Both logical OR (||) and bitwise OR (|) operators in Java evaluate boolean expressions. The key difference is that || is a short-circuit operator, meaning it evaluates the second operand only if the first is false. In contrast, | checks both operands regardless. For example, 'a>b||a++<c' checks only the first condition if true, leaving 'a' unchanged if it short-circuits, whereas 'a>b|a++<c' evaluates both, incrementing 'a' .

Arithmetic operators in Java enable computations by performing basic mathematical operations: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). In complex expressions, they combine to produce a computed result. For example, '10*10/5+3-1*4/2' uses multiple arithmetic operators to yield '21'. This involves applying operator precedence rules, evaluating multiplication and division before addition and subtraction .

Relational operators in Java are used to compare two values. They include '==', '!=', '>', '<', '>=', and '<='. For example, '==' checks if two values are equal, while '!=' checks for inequality. The operator '>' tests if the left operand is greater than the right, while '<' checks if it is less. '>=' and '<=' check for equality in addition to greater or less, respectively. Each operator returns a boolean result based on the comparison .

Unary operators in Java operate on a single operand to perform various operations such as incrementing/decrementing a value, negating an expression, or inverting a boolean value. Examples include increment (++), decrement (--), bitwise complement (~), and logical complement (!). In the first example, ++ and -- are used to adjust a numeric value. For instance, if 'int x = 10;', 'x++' outputs 10 (but increments x to 11), and '++x' outputs 12 .

Operator precedence in Java determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence, akin to the order of operations in mathematics. For instance, in the expression '10*10/5+3-1*4/2', multiplication and division are evaluated before addition and subtraction, resulting in a final output of 21. Parentheses can be used to explicitly specify evaluation order .

Compound assignment operators merge an operation and an assignment, like '+=', '-=', '*=', '/=', '%=', etc. They provide a shorthand to apply an operation to a variable and assign the result back to that variable. These operators can trigger implicit type conversion; if operands are of a different type, the result type may convert automatically to match the type of the left operand. For instance, 'a+=b' involves first converting the types to match and then executing the operation .

The logical AND (&&) operator in Java is a short-circuiting operator, meaning it evaluates the second operand only if the first operand is true. Conversely, the bitwise AND (&) does not short-circuit; it always evaluates both operands. For example, given 'int a=10; int c=20;', evaluating 'a<b&&a++<c' results in false, but 'a' remains 10 because the second condition is not evaluated. However, 'a<b&a++<c' also results in false, but 'a' becomes 11 as the increment operation is executed .

You might also like