Java Basic Operators

Java – Basic Operators

Operators are those which performs operation on operand.We will know about Baisc Java operators​ in this post.

Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible.

Characteristic of Java

Programming language​

  • Another programming language  using which we can develop applets, standalone applications, web applications and enterprise applications.

Platform Independent​

  • A Java program written and compiled on one machine can be executed on any other machine (irrespective of the operating system)

Object Oriented​

  • Complies to object oriented programming concepts. Your program is not object oriented unless you code that way

Compiled and Interpreted​

  • The .java file is compiled to a .class file & the .class file is interpreted to machine code​.

Java Operators – Types

An operator performs a particular operation on the operands it is applied on.

 

Types of  Baisc Java operators

  • Assignment Operators
  • Arithmetic Operators
  • Unary Operators
  • Equality Operators
  • Relational Operators
  • Conditional Operators
  • instaceof Operator
  • Bitwise Operators
  • Shift Operators

Operators – Assignment Operators/Arithmetic Operators

 

  • Assignment Operator

Operator

Description

Example

=

Assignment

int i = 10;

int j = i;

 

  • Arithmetic Operators

Operator

Description

Example

+

Addition

int i = 8 + 9; byte b = (byte) 5+4;

Subtraction

int i = 9 – 4;

Multiplication

int i = 8 * 6;

/

Division

int i = 10 / 2;

%

Remainder

int i = 10 % 3;

 

Operators – Unary Operators/Equality Operators

 

  • Unary Operators

Operator

Description

Example

+

Unary plus

int i = +1;

Unary minus

int i = -1;

++

Increment

int j = i++; 

Decrement

int j = i–;

!

Logical Not

boolean j = !true;

 

  • Equality Operators

Operator

Description

Example

==

Equality

If (i==1)

!=

Non equality

If (i != 4)

 

Operators – Relational Operators/Conditional Operators

 

  • Relational Operators

Operator

Description

Example

>

Greater than

if ( x > 4)

<

Less than

if ( x < 4)

>=

Greater than or equal to

if ( x >= 4)

<=

Less than or equal to

if ( x <= 4)

 

  • Conditional Operators ​(java || and Java &&)

Operator

Description

Example

&&

Conditional and

If (a == 4 && b == 5)

||

Conditional or

If (a == 4 || b == 5)

 

Operators – instanceof Operator/Bitwise Operators/shift operators

 

  • instanceof Operator

Operator

Description

Example

instanceof

Instance of

If (trenovision instance of website)

 

  • Bitwise Operators

Operator

Description

Example

&

Bitwise and

001 & 111 =  1 

|

Bitwise or

001 | 110 = 111

^

Bitwise ex-or

001 ^ 110 = 111

~

Reverse

~011 = -10

 

  • Shift Operators

Operator

Description

Example

>>

Right shift

4 >> 1 = 100 >> 1 =  010 = 2

<<

Left Shift

4 << 1 = 100 << 1 = 1000 = 8

>>>

Unsigned Right shift

4 >>> 1 = 100 >>> 1 = 010 = 2

 

Precedence of Java Operators

 

Category Operator Associativity
Postfix expression++ expression– Left to right
Unary ++expression –-expression +expression –expression ~ ! Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> >>> Left to right
Relational < > <= >= instanceof 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