Operator is a special symbol that tells the compiler to perform specific mathematical or logical Operation. Java supports following lists of operators.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
Arithmetic Operators
Given table shows all the Arithmetic operator supported by Java Language. Lets suppose variable A hold 8 and B hold 3.
Operator | Example (int A=8, B=3) | Result |
---|---|---|
+ | A+B | 11 |
- | A-B | 5 |
* | A*B | 24 |
/ | A/B | 2 |
% | A%4 | 0 |
Relational Operators
Which can be used to check the Condition, it always return true or false. Lets suppose variableA hold 8 and B hold 3.
Operators | Example (int A=8, B=3) | Result |
---|---|---|
< | A<B | False |
<= | A<=10 | True |
> | A>B | True |
>= | A<=B | False |
== | A== B | False |
!= | A!=(-4) | True |
Logical Operator
Which can be used to combine more than one Condition?. Suppose you want to combined two conditions A<B and B>C, then you need to use Logical Operator like (A<B) && (B>C). Here &&is Logical Operator.
Operator | Example (int A=8, B=3, C=-10) | Result |
---|---|---|
&& | (A<B) && (B>C) | False |
|| | (B!=-C) || (A==B) | True |
! | !(B<=-A) | True |
Truth table of Logical Operator
C1 | C2 | C1 && C2 | C1 || C2 | !C1 | !C2 |
---|---|---|---|---|---|
T | T | T | T | F | F |
T | F | F | T | F | T |
F | T | F | T | T | F |
F | F | F | F | T | T |
The Bitwise Operators:
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators:
Assume integer variable A holds 60 and variable B holds 13 then:
Example -
The following simple example program demonstrates the bitwise operators. Copy and paste the following Java program in Test.java file and compile and run this program:
public class Test { public static void main(String args[]) { int a = 60; /* 60 = 0011 1100 */ int b = 13; /* 13 = 0000 1101 */ int c = 0; c = a & b; /* 12 = 0000 1100 */ System.out.println("a & b = " + c ); c = a | b; /* 61 = 0011 1101 */ System.out.println("a | b = " + c ); c = a ^ b; /* 49 = 0011 0001 */ System.out.println("a ^ b = " + c ); c = ~a; /*-61 = 1100 0011 */ System.out.println("~a = " + c ); c = a << 2; /* 240 = 1111 0000 */ System.out.println("a << 2 = " + c ); c = a >> 2; /* 215 = 1111 */ System.out.println("a >> 2 = " + c ); c = a >>> 2; /* 215 = 0000 1111 */ System.out.println("a >>> 2 = " + c ); } }
This would produce the following result:
a & b = 12 a | b = 61 a ^ b = 49 ~a = -61 a << 2 = 240 a >> 15 a >>> 15
Assignment operators
Which can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3.
Operator | Example (int A=8, B=3) | Result |
---|---|---|
+= | A+=B or A=A+B | 11 |
-= | A-=3 or A=A+3 | 5 |
*= | A*=7 or A=A*7 | 56 |
/= | A/=B or A=A/B | 2 |
%= | A%=5 or A=A%5 | 3 |
=a=b | Value of b will be assigned to a |
Ternary operator
If any operator is used on three operands or variable is known as ternary operator. It can be represented with " ?: "
If any operator is used on three operands or variable is known as Ternary Operator. It can be represented with ? : . It is also called as conditional operator
Advantage of Ternary Operator
Using Ternary Operator reduce the number of line codes and improve the performance of application.
Syntax
expression-1 ? expression-2 : expression-3
In the above symbol expression-1 is condition and expression-2 and expression-3 will be either value or variable or statement or any mathematical expression. If condition will be true expression-2 will be execute otherwise expression-3 will be executed.
Syntax
a<b ? printf("a is less") : printf("a is greater");
Flow Diagram
Find largest number among 3 numbers using ternary operator
Example
#include<stdio.h> #include<conio.h> void main() { int a, b, c, large; clrscr(); printf("Enter any three number: "); scanf("%d%d%d",&a,&b,&c); large=a>b ? (a>c?a:c) : (b>c?b:c); printf("Largest Number is: %d",large); getch(); }
Output
Enter any three number: 5 7 2 Largest number is 7
Using break statement : When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. Here is a simple example:
/**
* This program demonstrates
* break to exit a loop.
*/
public class BreakDemo
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break; // terminate loop if i is 5
}
System.out.print(i + " ");
}
System.out.println("Loop is over.");
}
}
Output :
1 2 3 4 Loop is over.
Using continue statement : When a continue statement is encountered inside the body of a loop, remaining statements are skipped and loop proceeds with the next iteration. Here is a simple example.
/**
* This program demonstrates continue
* to skip remaining statements of iteration.
*/
public class ContinueDemo
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue; // skip next statement if i is even
}
System.out.println(i + " ");
}
}
}
Output :
1 3 5 7 9
No comments:
Post a Comment