0% found this document useful (0 votes)
18 views6 pages

Acharya Parambarai

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

Acharya Parambarai

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1) JAVA Operators

Operator is a symbol is used to perform some operations with the help of


operands
Operator , operations, operands
Symbol, process, variables

Suppose a+b means

a,b are Operands + is a operator addition is an Operation

Three types of operators


a) Unary operator (Unary operations performed with the help of only one operand)
b) Binary Operator (two operands)
c) Ternary Operator (three operands)

Operator Type Category Precedence

Unary Postfix Increment / expr++ expr--


Decrement

Prefix Increment / Decrement ++expr --expr +expr -expr ~ !

Arithmetic multiplicative */%

Additive +-

Shift Shift << >> >>>

Relational comparison < > <= >= instanceof

Equality == !=

Bitwise bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

Logical logical AND &&

logical OR ||

Ternary Ternary ?:

Assignment assignment = += -= *= /= %= &= ^= |=


<<= >>= >>>=
i) Unary operator

1) Increment Operator - ++
int a=5
++ a ( it increment one value to the variable a) now a= 6

2) Decrement Operator - - -
int a=5
- -a ( it decrement one value to the variable a) now a= 4

Types
Pre increment / pre decrement
Post increment / Post decrement

3) Minus Operator -
int a=5
Negative value of the a now a= -5

Example Unary operator


class Unary {
public static void main(String args[]) {
int x=10;
System.out.println(x++); //10 (11)
System.out.println(++x); //12
System.out.println(x--); //12 (11)
System.out.println(--x); //10
}
}

ii) Binary Operators

1) Java Arithmetic Operator Example


class BinaryArith{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+1); //11 sum
System.out.println(a-b); //5 difference
System.out.println(a*b); //50 multiply
System.out.println(a/b); //2 division quotient
System.out.println(a%b); //0 division remainder
}
}

2) Java Left Shift Operator Example


Op1 << op2 ( left shift operator)
Op1 * power(2,op2)
Op1 >> op2 (right shift operator)
Op1 / power(2,op2)
class BinaryLeftShift
{
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
System.out.println(20<<2); //20*2^2=20*4=80
System.out.println(15<<4); //15*2^4=15*16=240
}

3) Java Right Shift Operator Example


class BinaryRightShift
{
public static void main(String args[])
{
System.out.println(10>>2); //10/2^2=10/4=2
System.out.println(20>>2); //20/2^2=20/4=5
System.out.println(20>>3); //20/2^3=20/8=2
}
}
4) JAVA Logical and Bitwise Operator

Java AND Operator Example: Logical && and Bitwise &


The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.
&& - > logical and ( check the condition if both are true return true or Return false
& -> check both the condition even true or false
|| - logical OR | - bitwise OR

JAVA logical and bitwise OR operator

class BinaryLogicalAnd
{
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
//10<5 && 10 < 20
// False && True = False
System.out.println(a<b&a<c); //false & true = false

}
}
5) Java AND Operator Example: Logical && vs Bitwise &
The logical && operator doesn't check second condition if first condition is false. It checks
second condition only if first one is true.
The bitwise & operator always checks both conditions whether first condition is true or false.

Java Logical and bitwise AND operator


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
// 10<5 && 10++(11) < 20
// 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
}

}
6) Assignment and Short hand operator
Short hand operator
a = a+ b ( equal to) a+=b

Java Assignment Operator Example


class BinaryAssignmentShort{
public static void main(String[] args){
int a=10;
a+=3; //10+3 a= a+ 3 a = 10 + 3 = 13
System.out.println(a);
a-=4; //13-4 a = a -4 13 – 4 = 9
System.out.println(a);
a*=2; //9*2 = 18
System.out.println(a);
a/=2; //18/2 = 9
System.out.println(a);
}
}
iii) Ternary Operator or Conditional Operator
class Ternary {
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)?a:b;
System.out.println(min);
}
}
Explanation

check the condition ? True part : False part

a< b ? a : b

2< 5 ? 2 : 5

True : 2 : 5

Java Operators Precedence and Associativity

Precedence and associativity are two features of Java operators. When there are two or more
operators in an expression, the operator with the highest priority will be executed first.

Associativity specifies the order in which operators are executed, which can be left to right or
right to left. For example, in the phrase a = b = c = 8, the assignment operator is used from
right to left. It means that the value 8 is assigned to c, then c is assigned to b, and at last b is
assigned to a. This phrase can be parenthesized as (a = (b = (c = 8)).

The priority of a Java operator can be modified by putting parenthesis around the lower order
priority operator, but not the associativity. In the equation (1 + 2) * 3, for example, the addition
will be performed first since parentheses take precedence over the multiplication operator.

Operator Precedence in Java (Highest to Lowest)

Category Operators Associativity

Postfix ++ - - Left to right

Unary + - ! ~ ++ - - Right to left

Multiplicative */% Left to right

Additive +- Left to right

Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right


Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Operator Precedence Vs. Operator Associativity

The operator's precedence refers to the order in which operators are evaluated within an
expression whereas associativity refers to the order in which the consecutive operators within
the same group are carried out.

Precedence rules specify the priority (which operators will be evaluated first) of operators.

Example: Operator Precedence

class Precedence {
public static void main(String[] args)
{
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy