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

19 comments:

  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    Click here:
    angularjs training in btm
    angularjs training in rajajinagar |angularjs training in marathahalli

    ReplyDelete
  2. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    Click here:
    Microsoft azure training in velarchery
    Click here:
    Microsoft azure training in sollinganallur

    ReplyDelete
  3. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favourites blog site list and will be checking back soon.

    Data Science Training in Chennai | Data Science training in anna nagar
    Data Science training in chennai | Data science training in Bangalore
    Data Science training in marathahalli | Data Science training in btm

    ReplyDelete
  4. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me.

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  5. I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  6. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  7. It s a very useful page. Thank you. f0511030f52716e34682f12d7f542c23
    daday
    ermenek
    kavaklidere
    marmaris
    tonya
    ergene
    kurtun
    abana
    torbali

    ReplyDelete
  8. Congratulations on your article, it was very helpful and successful. a536d499dff30976b03af398e0af8cb3
    website kurma
    website kurma
    numara onay

    ReplyDelete
  9. Thank you for your explanation, very good content. 5f9e1a740ab51fc942b0ff7d003e82dd
    define dedektörü

    ReplyDelete
  10. Thanks for your article. 579fd512e3a1839bdbf4b4cf1f6148e1
    evden iş imkanı

    ReplyDelete

ShareThis