yt

Header Ads

Chapte 9 What is Operator and Types Explain in Details

Post Intro:- So Frirnds Fainaly Hum log ab es post main Operator Kiya hota hai and Kitne Types ke hota hai, and kaise declared karna hai, and a propar example ke sath dekne bale hai.

Operator :

Operator is a specail symbol used for find logical and Mathematical Operation on data or variable in program.

Operand :- 

Operand is a data or variable on which the operation is to be perfromed.

Types of Operator:

  1. ⇒Arithmetic Operators
  2. ⇒Relational Operators
  3. ⇒Logical Operators
  4. ⇒Assignment Operators
  5. ⇒Bitwise Operators
  6. ⇒Increment/ Decrement Operators
  7. ⇒Conditional Operators 
Arithmetic Operators :  Used for mathematical operation.

Symbol

Operation

Examples

+

Addition

a + b

-

Subtraction

a- b

*

Multiplication

a*b

/

Division

a/b

%

Modulus

a % b


Example of Arithmetic Operator :


class Tech369kk
{
 public static void main(String[] args)
 {
 int a=10,b=3;
 System.out.println("Add="+(a+b));
 System.out.println("Sub="+(a-b));
 System.out.println("Multi="+(a*b));
 System.out.println("Div="+(a/b));
//  Note:-modulus(%) always holds remainder value  i mean ses
 System.out.println("Mod="+(a%b));
 }
}


Output : 


PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> javac Blog.java
PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Tech369kk
Add=13
Sub=7
Multi=30
Div=3
Mod=1     // Given Reminder




// Get user input find only addition value

import java.util.Scanner;
class Bloog{
  public static void main(String[] args) {
    Scanner obj = new Scanner(System.in);
    System.out.println("Enter a value : ");
    int a=obj.nextLine();
    System.out.println("Enter b value : ");
    int b=obj.nextLine();
    int c= a+b;
    System.out.println("Total Addition value is :" +c);
  }
}



Relational Operators:  Used for Comaparison  Operations 

  • Here:     0 means False
  •                1 means True

Symbol

Operation

Examples

==

Equal to

2==3 return 0

!=

Not equal to

2!=3 return 1

> 

Greater than

2>3 return 0

< 

Less than

2<3 return 1


>=

Greater than

or

equal to


2>=3 return 0


<=

Less than

or

equal to


2<=3 return 1



Example of RelatiOperator :


class Relational{
 public static void main(String[] args){
    int a=10,b=5;
   
    // if (a==b){     //False
    // if (a!=b){     //True
    // if (a>b){        //True  
    // if (a<b){       // False
    // if (a>=b){       // True
    if (a<=b){          // False
        System.out.println("Return 0 means True");
    }
    else{
        System.out.println("Return 1 Means False");
    }
}
}



Logical Operators:  Used for Conditional  Operations 

Symbol

Operation

Examples

&&

AND

if(a>b && x>z)

||

OR

if(x>y || x>z)

!

NOT

If(!(x>y))

 Note:- 

  • (a>b) && (a > c) Here this expression returns true if both conditions are true.
  • (a>b) || (b>c) Here this expression returns true if any one or both conditions are true.
  • !(a>b) Not operator reverses the state means if the condition is true it returns false and if the condition is false it returns true  .
Example of Logical Operators
  • A. And Operator (&&)


A. And Operator (&&)
class Logical{
public static void main(String[] args)
{
 int a=10,b=30,c=40;
if(a>b && a>c){
    System.out.println("a is greatest");
}
 if(b>a && b>c){
    System.out.println("b is greatest");
}
 if(c>a && c>b){
    System.out.println("c is greatest");
}
}
}


Output :

C is greatest


  • B. Or Operator (||)


B. Or Operator(||)
class Logical{
public static void main(String[] args)
{
 int a=10,b=30,c=40;
if(a>c || a>c){
    System.out.println("a is greatest");
}
 if(b>a || b>c){
    System.out.println("b is greatest");
}
 if(c>a || c>b){
    System.out.println("c is greatest");
}
}
}



Output :


PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOG> java Logical  
b is greatest
c is greatest



Same as used C. Or Not Operator In Java


Assignment Operators :   

Symbol

Example

Same As

=

x=y

x=y

+=

x+=y

x=x + y

-=

x-=y

x=x-y

=

x=y

x=x*y

/=

x/=y

x=x/y

%=

x%=y

x=x%y

Example of Assignment Operator


class Assignment
{
 public static void main(String[] args)
 {
    int a=10,b=4, c;
    // a+=b;  or
    c=a+b;
    System.out.println("Addition of a and b:"+c);

    int x2=5,y2=3;
    x2-=y2;//x2=x2-y2
    System.out.println(x2);

    int x3=5,y3=3;
    x3*=y3;//x3=x3*y3
    System.out.println(x3);

    int x4=5,y4=3;
    x4/=y4;//x4=x4/y4
    System.out.println(x4);

    int x5=5,y5=3;
    x5%=y5;//x5=x5%y5
    System.out.println(x5);
 }
}



Output:-


Addition of a and b:14
2
15
1
2



Bitwise Operators :   

Symbol

Operation

Example

&

AND

x&y

|

OR

x|y

<< 

Shift Left

X<<2

>> 

Shift Right

x>>2

^

X-OR

x^y


Example of Bitwise Operator 


class Bitwise
{
 public static void main(String[] args)
 {
     //variable declaration
    int a=10,b=3,c;
    c=a&b;      //AND Operation
    System.out.println("a&b="+c);
    c=a|b;      //OR Operation
    System.out.println("a|b="+c);
    c=a>>2;     //Shift right Operation
    System.out.println("a>>2="+c);
    c=a<<2;     //Shift left Operation
    System.out.println("a<<2="+c);
    c=a^2;      //X-OR Operation
    System.out.println("a^2="+c);
 }
}


Output 


PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOg> java Bitwise
a&b=2
a|b=11
a>>2=2
a<<2=40
a^2=8



Increment/Decrement Operators :   

Symbol

Name

Function

Example

++

Increment

It increments the value by 1

++x

--

Decrement

It decrement the value by 1

--x


Example of Increment And Decrement:- 


class IncDec
{
 public static void main(String[] args)
 {//variable declaration
  int a=5,b=10;
   System.out.println(++a);
   System.out.println(--b);
 }
}


Output :

PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOg> java IncDec  
6
9



Conditional  Operators :- If the condition is true second part will execute otherwise third part will execute.

Example of Condiitonal Operators:


Q. Check Greater Number
class Ternarry{
  public static void main(String[] args){
    int a=5,b=10,max; // Variable declaration
     //same as min
    max=a>b?a:b;
    System.out.println("Greater value is "+ max);  
    //don't be confused,here + is separator in java
  }
}



Output :- 

PS D:\Learning File\BCA\BCA 3rd Sem\JAVA OOP\BLOg> java Ternarry
Greater value is 10

I hope this is helpful for you thanks.

No comments

Theme images by Dizzo. Powered by Blogger.