OPERATORS AND EXPRESSIONS

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
operator in java

Arithmetic Operators

Given table shows all the Arithmetic operator supported by Java Language. Lets suppose variable A hold 8 and B hold 3.
OperatorExample (int A=8, B=3)Result
+A+B11
-A-B5
*A*B24
/A/B2
%A%40

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.
OperatorsExample (int A=8, B=3)Result
<A<BFalse
<=A<=10True
>A>BTrue
>=A<=BFalse
==A== BFalse
!=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.
OperatorExample (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

C1C2C1 && C2C1 || C2!C1!C2
TTTTFF
TFFTFT
FTFTTF
FFFFTT


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.
OperatorExample (int A=8, B=3)Result
+=A+=B or A=A+B11
-=A-=3 or A=A+35
*=A*=7 or A=A*756
/=A/=B or A=A/B2
%=A%=5 or A=A%53
=a=bValue 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

Ternary Operator

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

2 comments:

  1. 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