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

Java Operators Explained: Types & Examples

Operators in java

Uploaded by

chaitanyadogga0
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)
18 views10 pages

Java Operators Explained: Types & Examples

Operators in java

Uploaded by

chaitanyadogga0
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 Type Category Precedence

Unary postfix expr++ expr--

prefix ++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 = += -= *= /= %= &= ^= |= <<= >>= >>>=

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 --

1. public class OperatorExample{


2. public static void main(String args[]){
3. int x=10;
4. [Link](x++);//10 (11)
5. [Link](++x);//12
6. [Link](x--);//12 (11)
7. [Link](--x);//10
8. }}

Output:

10
12
12
10

Java Unary Operator Example 2: ++ and --

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=10;
5. [Link](a++ + ++a);//10+12=22
6. [Link](b++ + b++);//10+11=21
7.
8. }}

Output:

22
21

Java Unary Operator Example: ~ and !

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=-10;
5. boolean c=true;
6. boolean d=false;
7. [Link](~a);//-11 (minus of total positive value which star
ts from 0)
8. [Link](~b);//9 (positive of total minus, positive starts fro
m 0)
9. [Link](!c);//false (opposite of boolean 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

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. [Link](a+b);//15
6. [Link](a-b);//5
7. [Link](a*b);//50
8. [Link](a/b);//2
9. [Link](a%b);//0
10. }}

Output:

15
5
50
2
0

Java Arithmetic Operator Example: Expression

1. public class OperatorExample{


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

Output:

ADVERTISEMENT

21

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

1. public class OperatorExample{


2. public static void main(String args[]){
3. [Link](10<<2);//10*2^2=10*4=40
4. [Link](10<<3);//10*2^3=10*8=80
5. [Link](20<<2);//20*2^2=20*4=80
6. [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

1. public OperatorExample{
2. public static void main(String args[]){
3. [Link](10>>2);//10/2^2=10/4=2
4. [Link](20>>2);//20/2^2=20/4=5
5. [Link](20>>3);//20/2^3=20/8=2
6. }}

Output:

2
5
2

Java Shift Operator Example: >> vs >>>

1. public class OperatorExample{


2. public static void main(String args[]){
3. //For positive number, >> and >>> works same
4. [Link](20>>2);
5. [Link](20>>>2);
6. //For negative number, >>> changes parity bit (MSB) to 0
7. [Link](-20>>2);
8. [Link](-20>>>2);
9. }}

Output:

5
5
-5
1073741819

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.

ADVERTISEMENT
ADVERTISEMENT

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. [Link](a<b&&a<c);//false && true = false
7. [Link](a<b&a<c);//false & true = false
8. }}

Output:

false
false

Java AND Operator Example: Logical && vs Bitwise &

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. [Link](a<b&&a++<c);//false && true = false
7. [Link](a);//10 because second condition is not checked
8. [Link](a<b&a++<c);//false && true = false
9. [Link](a);//11 because second condition 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.

ADVERTISEMENT
ADVERTISEMENT

The bitwise | operator always checks both conditions whether first


condition is true or false.

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. [Link](a>b||a<c);//true || true = true
7. [Link](a>b|a<c);//true | true = true
8. //|| vs |
9. [Link](a>b||a++<c);//true || true = true
10. [Link](a);//10 because second condition is not che
cked
11. [Link](a>b|a++<c);//true | true = true
12. [Link](a);//11 because second condition is checke
d
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

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=2;
4. int b=5;
5. int min=(a<b)?a:b;
6. [Link](min);
7. }}

Output:

Another Example:

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int min=(a<b)?a:b;
6. [Link](min);
7. }}

Output:

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

1. public class OperatorExample{


2. public static void main(String args[]){
3. int a=10;
4. int b=20;
5. a+=4;//a=a+4 (a=10+4)
6. b-=4;//b=b-4 (b=20-4)
7. [Link](a);
8. [Link](b);
9. }}

Output:

14
16

Java Assignment Operator Example

1. public class OperatorExample{


2. public static void main(String[] args){
3. int a=10;
4. a+=3;//10+3
5. [Link](a);
6. a-=4;//13-4
7. [Link](a);
8. a*=2;//9*2
9. [Link](a);
10. a/=2;//18/2
11. [Link](a);
12. }}

Output:

13
9
18
9

Java Assignment Operator Example: Adding short

1. public class OperatorExample{


2. public static void main(String args[]){
3. short a=10;
4. short b=10;
5. //a+=b;//a=a+b internally so fine
6. a=a+b;//Compile time error because 10+10=20 now int
7. [Link](a);
8. }}

Output:
Compile time error

After type cast:

1. public class OperatorExample{


2. public static void main(String args[]){
3. short a=10;
4. short b=10;
5. a=(short)(a+b);//20 which is int now converted to short
6. [Link](a);
7. }}

Output:

20

Common questions

Powered by AI

Java allows combining the assignment operator with other operators like addition, subtraction, multiplication, and so on, enabling compound assignments that perform the operation and assignment in one step. These include operators like +=, -=, *=, /=, etc. For example, using `a += 4` adds 4 to the current value of `a` and assigns the result back to `a`, equivalent to `a = a + 4`. This simplifies code and reduces verbosity, promoting readable and efficient operations. Similarly, other compound operations include `a *= 2` which doubles `a` .

In Java, the unary operators "++" and "--" can be applied in prefix (++x, --x) or postfix (x++, x--) forms, affecting the evaluation order. In postfix form, the current value is used in the expression before the variable is incremented or decremented. For example, if x is 10, x++ results in a value of 10, but x becomes 11 after evaluation. Conversely, in prefix form, the variable is incremented or decremented first before being used in the expression. Thus, for ++x when x is 10, the result is 11 immediately .

Operator precedence in Java determines the order in which operators are evaluated in expressions. Arithmetic operators are subject to standard precedence rules: multiplicative operations (*, /, %) are evaluated before additive ones (+, -). Without explicit parentheses, a mixed-type operation like `10*10/5+3-1*4/2` evaluates as follows: first the multiplication (10*10 and 1*4), then the divisions (100/5 and 4/2), followed by addition and subtraction in left-to-right order, ultimately resulting in 21 . Precedence ensures predictable calculation order unless overridden by parentheses.

The bitwise NOT operator '~' inverts all the bits of its operand. For an integer, it flips each bit, changing 0 to 1 and 1 to 0, effectively calculating the two's complement of the number, which is equivalent to -(n+1) for any integer n. For example, if the integer 10 (binary 1010) is passed through '~', the result is -11, because the bitwise inversion results in binary 11111111111111111111111111110101, which represents -11 in two's complement form .

The bitwise AND operator '&' and the logical AND operator '&&' differ primarily in their evaluation strategy. The logical AND '&&' operator short-circuits, meaning if the first operand evaluates to false, the second operand is not evaluated at all. For example, in 'a && b', if 'a' is false, 'b' is not checked . In contrast, the bitwise AND '&' operator always evaluates both operands regardless of the outcome of the first operand. Consequently, 'a & b' will evaluate both 'a' and 'b', potentially leading to different side effects or performance implications .

The right shift operator '>>' maintains the sign bit (i.e., it does arithmetic shift), preserving the sign of the original integer, and fills the shifted positions with the original sign bit, either 0 or 1 depending on whether the number is positive or negative . In contrast, the unsigned right shift operator '>>>' shifts zeros into the leftmost positions, changing the original sign bit to 0, which can significantly alter the value for negative integers. For example, shifting -20 >> 2 results in -5, retaining the sign, while -20 >>> 2 results in a very large positive number, specifically 1073741819, due to the change of the sign bit .

The ternary operator in Java succinctly replaces simple if-then-else statements with a single line of code. Its syntax is `condition ? expression1 : expression2`, where the condition is evaluated; if true, `expression1` is executed, otherwise `expression2` is executed. This condenses logic into a single line, boosting readability and efficiency. For example, `int min = (a < b) ? a : b;` assigns `min` the value of `a` if `a` is less than `b`, otherwise assigns `b` .

The logical OR operator '||' short-circuits evaluations, meaning if the first operand yields true, the second operand is not evaluated, improving efficiency in conditional checks . In contrast, the bitwise OR operator '|' always evaluates both operands regardless of the first operand's outcome, potentially leading to unnecessary operations or side effects if the operands have side effects . This means logical OR is preferable in scenarios where short-circuit behavior is desired to avoid evaluation of expensive or non-idempotent operations when not needed.

The left shift operator '<<' in Java shifts the bits of an integer to the left by the number of positions specified, inserting zeros on the right. This operation is equivalent to multiplying the number by 2 raised to the power of the number of positions shifted. For instance, if you apply 10<<2, the number 10 (binary 1010) becomes 40 (binary 101000). Each left shift effectively doubles the number, allowing compact multiplication by powers of two .

While logical operators (&&, ||) offer potential performance benefits through short-circuiting (only evaluating the necessary operands), they may lead to missed operations if side effects are present in unevaluated expressions. Bitwise operators (&, |), on the other hand, always evaluate both operands, which could either preserve necessary evaluations or lead to inefficiency due to unnecessary operand checking. This distinction is critical for writing efficient and correct logic, particularly if operand evaluation incurs overhead or side effects. Choosing the operator type based on context—whether explicit evaluation or conditional performance boost—is crucial for ensuring program integrity and efficiency .

You might also like