Go to Top

Tuesday, 26 April 2011

Operators in Java

                                            The symbols used in a Java expression are known as Operators, whereas the values on which the operations are performed are known as Operands. There are three types of operators :

1. Unary Operator : These are the operators which require only one operand to perform the operation.
2. Binary Operator : These are the operators which require two operands to perform the operation.
3. Tertiary Operator : These are the operators which require three operands to perform the operation.

                                            The operators are also classified on the basis of their types of operation :

1. Arithmetical Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operator
6. Compound Assignment Operator (Short Hand Operator)


1. Arithmetical Operators : These operators are required for arithmetical calculations. They are of two types :


1. Binary Operators : The table given below lists all binary form arithmetical operators in Java :

Operator
Description
Function
Example
+
Plus
Adds the operands
a+b
-
Minus
Subtracts the operands
a-b
*
Multiply
Multiplies the operands
a*b
/
Divide
Divides the operands
a/b
%
Modulus (Remainder)
Finds the remainder when one number is divided by the other
a%b

2. Unary Operators : Operators + and - have a unary form too, in addition to binary forms. They are illustrated along with their functions in the table given below :

Operator
Description
Example
+
Promotes a to int if it’s a byte, short, or char
+a
-
Arithmetically negates a
-a

                                            The operators ++ and -- are the two shortcut arithmetic operators. They are used as follows :
1. x++ stands for x=x+1.
2. x-- stands for x=x-1.
                                            They have two versions :

1. The Prefix version : Increments/decrements the value of the operand by 1 before doing the operation with it.
1. The Postfix version : Increments/decrements the value of the operand by 1 after doing the operation with it.
                                            The table given below shows these operators :

Operator
Description
Example
++
Increments a by 1 after performing the operation with it
a++
++
Increments a by 1 before performing the operation with it
++a
--
Decrements a by 1 after performing the operation with it
a--
--
Decrements a by 1 before performing the operation with it
--a

2. Relational Operators : The relational operators in Java are used to compare the values of two operands a and b and results in the outcome of a boolean value. These are given in the table below :

Operator
Description
Example
> 
Greater than
a>b
< 
Less than
a<b
>=
Greater then equal to
a>=b
<=
Less than equal to
a<=b
= =
Equal to
a= =b
!=
Not equal to
a!=b

3. Logical Operators : These operators in Java are used to return a boolean value from an expression containing two or more comparisons. Java supports six logical operators, out of which five are binary and one is unary. The table belows shows these operators :


Operator
Description
Example
&&
Short-circuit or conditional AND
a&&b
||
Short-circuit or conditional OR
a||b
!
Logical NOT
!a
&
Logical AND
a&b
|
Logical OR
a|b
^
Logical XOR (exclusive OR)
a^b

5. Assignment Operator : The assignment operator = is used to assign value to a variable or to initialize them. For example :

int a=10;
float b=99.99;

6. Compound Assignment Operator (Short Hand Operator) : These operators are just like a shortcut assignment operator. They are given in the table below :

Compound Assignment Operator
Arithmetic Operator
a+=b
a=a+b
a-=b
a=a-b
a*=b
a=a*b
a/=b
a=a/b

2 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete

ShareThis