Saturday, June 01, 2013

Java Tutorial : Operator and Expressions in Java

Leave a Comment Follow us on twitter
In this java tutorial we are going to see how Operators and Expressions are used in java programming. In the computer programming, Operator and Expressions play a vital role and are almost same for all the programming languages. Let's discuss the Operators first. So, basically java operators are grouped into four categories and they are : 

-Arithmetic Operator
-Bitwise Operator
-Relational Operator
-Logical Operator

Without these operators, a coder cannot do the mathematical computation. Let's see each one of them individually.

* Arithmetic Operator : +, -, *, /, %, ++, +=, --, -=, *=, /=, %= are arithmetic operators used in java programming.

Eg.:
class Sample
{
public static void main(String args[])
{
int a=2,b;
b=a*2;
System.out.println("Value of b after using Arithmetic operator is :"+b);
}
}

* Bitwise Operator : ~, &, |, ^, >>, >>>, <<, &=, |=, ^=, >>=, >>>=, <<= are the bitwise operators currently using in Java Programming.

Eg.:
Suppose we have a binary value
01000001  :  65
Apply <<2 (i.e. Left Shift)
00000100  :  4

* Relational Operator : ==, !=, >, <, >=, <= are the relational operators. These are used to determine the relations between the operands.

Eg.:
int a=5, b=1;
boolean d=a>b;
If the result of a>b which is True is stored in variable d.

* Logical Operator : &, |, ^, ||, &&, !, &=, |=, ^=, ==, !=, ?: are the logical operators (boolean logical operators). In this we generally use assignment and ? operator.

-Boolean means the value will be either true or false.

Eg.:
int a,b;
if(a>b)?a=1:b=1;

In this example if the value of a is greater than b then, value assigned to a equals to 1. In the else case b=1.


Now let's see Expressions in Java Programming. Expression in Java Programming is same as the expression in Computer Programming. Expression is the combination of operands and operators. Every computer programming languages have its own rules for Expression, but they are all like to be similar.

Eg.:
-a+5 is an expression.
-a+b+c is an expression.
-a+b+5 is an expression.

I hope that these java tutorial will help you to learn java. Java have a wider scope in Computer Programming and quite general in use in today's world.

0 comments:

Post a Comment