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
this article helps in many ways.Thankyou so much.
ReplyDeletejavascript training in chennai
javascript training in Adyar
core java training in chennai
core java Training in Tnagar
C++ Training in Chennai
C C++ Training in Adyar
core java training in chennai
core java training institutes in chennai
Is Gemini 2fa not working appropriately? It is safe to say that you are going up against various blunders as 2fa not working appropriately? In the event that indeed, you can generally accomplish the best arrangement from somebody who has direction over Gemini and has incredible learning in managing the mistakes. Dial Gemini customer care number which is continually working and clients can advance their inquiries to the experts by means of this gateway in the most effortless way.
ReplyDelete