Complete Java masterclass for beginners -07 (Operators)

Operators in Java

Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in Java which are given below:

Unary Operator,
Arithmetic Operator,
Shift Operator,
Relational Operator,
Bitwise Operator,
Logical Operator,
Ternary Operator and
Assignment Operator.
Java Operator Precedence



Java Unary Operator

The Java unary operators require only one operand. Unary operators are used to perform various operations i.e.:

incrementing/decrementing a value by one
negating an expression
inverting the value of a boolean

Java Unary Operator Example: ++ and --





Java Unary Operator Example: ~ and !





Java Arithmetic Operator Example: Expression




Java Right Shift Operator

The Java right shift operator >> is used to move left operands value to right by the number of bits specified by the right operand.

Java Right Shift Operator Example





Java Shift Operator Example: >> vs >>>


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.



Java AND Operator Example: Logical && vs Bitwise &




Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn't check second condition if first condition is true. It checks second condition only if first one is false.

The bitwise | operator always checks both conditions whether first condition is true or false.



Java Ternary Operator

Java Ternary operator is used as one liner 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



Java Assignment Operator

Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.

Java Assignment Operator Example



Java Assignment Operator Example



Java Assignment Operator Example: Adding short



After type cast:




Next topic: Java keywords.
Happy coding

Comments

Popular posts from this blog

Complete Java masterclass for beginners -10 (switch statement)

Complete Java masterclass for beginners -11 (for loop)

Complete Java masterclass for beginners -06 (Unicode System)