Operator Concept in Java:-


It is used to perform operations using operand, the operand can be variable, constant, or literals.

int a=10;  a is the variable,10 is the literals

System.out.println(100+200) ;  100 ,200 is called literals.
.....................................................................................................................................

The operator will be called by a symbol but defined as a method, for example, if we use + operator then + is the symbol but it has defined as a +() method

Operator +(param1)
{

}


Type of Operator:-

1) Unary Operator:- 
This operator will work using a single operand.


1.1 Increment:-  increase current value by one

1.1.1  Post Increment:-       operand++

     First complete other operation(print, assign) then increase the value

       int a=5;
       int b;
       b=a++;
       System.out.println(a,",",b);
   
The expected output is b=5 and a=6

1.1.2   Pre Increment:-        ++operand

      First increase value by one after that complete other operation

       int a=5;
       int b;
       b=++a;
       System.out.println(a,",",b);


class Incre
{
   public static void main(String args[])
   {
      int a=2,b=3;  
    
      System.out.println(a++  + "," + ++b );

   }

}


The expected output is  a=2 and b=4


Example of Increment |Decrement:-

class Incre
{
   public static void main(String args[])
   {
      int a=2,b=3; 
      b= a++ + a++ + a++ + a++;
  //   14   2      3        4        5           6


      System.out.println(a  + "," + b);

   }

}


class Incre
{
   public static void main(String args[])
   {
      int a=2,b=3;  
      b = ++a + a++ + ++a + a++;
  // 16      3     3      5    5  6


    System.out.println(a  + "," + b);

   }

}

1.2 Decrement


class Incre
{
   public static void main(String args[])
   {
      int a=12,b=3; 
      b = --a + a-- + --a + a--;
   //  40      11    11     9   9  8
      System.out.println(a  + "," + b);

   }

}

2) Binary  Operator:-

It requires a minimum of two operands to perform the operation.

2.1 ) Arithmetic Operator:-  +,-,*,/,%
2.2)  Conditional Operator:-   <,>,<=,>=
2.3)  Comparison Operator:-  ==
2.4)  Assignment Operator
       2.4.1)  Simple     =
       2.4.2)  Complex  +=,-=,*=,/=,%=

         a+=7  or a=a+7
         a-=3   or a=a-3

2.5) Logical Operator:-    &&,||,!

     && :-  it return true when all condition will be true
      ||     :-  it return true when particular condition will be true
      !     :-  it is opposite of true statement and converted result into false statement
      
a=9   
    a>2 && a<5
     a>2 or a<5

3) Ternary Operator:-

     It is used to solve the condition-based problems using a single-line statement. Ternary operators contain a minimum of three operators to solve statements.

res = (condition)?true:false;


WAP to check that the entered number is even or odd using the ternary operator

class CheckEvenOdd
{
    public static void main(String args[])
    {
        int num=4;
        String s = (num%2==0)?"Even":"Odd";
        System.out.println(s);
       
        

    }


}

class Checkgreater
{
   public static void main(String args[])
   {
      int a=2,b=5;
      String s = (a>b)?"a is greater":"b is greater";
      System.out.println(s);


   }

}


WAP to calculate electricity bill where unit price and total consumption will be assigned by the user? 

WAP to calculate the area of the circle?

WAP to reverse a five-digit number where the first digit and last digit will be the same?

eg:-  12345  o/p  14325

WAP to perform the addition of complex numbers?

eg:   2+3i, 4+5i  = 6+8i

WAP to check vowel and consonant?

WAP to check Leap Year?

WAP to check that today is Sunday or not?

WAP to check that number is divisible by 3 and 5 both or not?

WAP to check that char is numeric or alphabets?

 WAP to check the greatest number?

hint:- 

int a=2,b=45,c=30;

String s = (a>b && a>c)? "a is greatest":(b>c)?"b is greatest":"c is greatest";




9 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. import java.util.Scanner;
    class Checknumber
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    char ch;
    System.out.println("Enter char");
    ch=sc.next().charAt(0); // "h"
    String s = (ch>=48 && ch<=57)?"numeric":"other";
    System.out.println(s);


    }


    }

    ReplyDelete
  2. Program of Greatest Number Solved in class by me import java.util.Scanner;
    class Checkgrt
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int a=1000,b=2000,c=3000;
    String s = (a>b && a>c)?"a is greatest":(b>c?" b is greatest":" c is

    greatest");
    System.out.println(s);


    }


    }

    ReplyDelete
  3. class Revv
    {
    public static void main(String args[])
    {
    int a=12345;
    int d1,d2,d3,d4,d5,rev,temp;
    temp=a;
    d5=a%10;
    a=a/10;
    d4=a%10;
    a=a/10;
    d3=a%10;
    a=a/10;
    d2=a%10;
    a=a/10;
    d1=a;
    a=temp;
    rev=d1*10000+d4*1000+d3*100+d2*10+d5;
    System.out.println("The reverse of "+a+" without changing the first and the last digit is: "+rev);
    }
    }

    ReplyDelete
  4. class Vowel
    {
    public static void main(String args[])
    {
    char ch='m';
    int i=0;
    switch(ch)
    {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':i++;
    }
    if(i==1)
    {
    System.out.println(ch+" is a Vowel");
    }
    else
    {
    if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
    {
    System.out.println(ch+" is a Consonent");
    }
    else
    {
    System.out.println(ch+" is not an alphabet");
    }
    }
    }
    }

    ReplyDelete
  5. class Leap
    {
    public static void main(String args[])
    {
    int year=1900;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
    {
    System.out.println("Leap Year");
    }
    else
    {
    System.out.println("Not a Leap Year");
    }
    }
    }

    ReplyDelete
  6. class Sunday
    {
    public static void main(String args[])
    {
    String s= "monday";
    if(s=="sunday")
    {
    System.out.println("Weekend yay!");
    }
    else
    {
    System.out.println("Not a holiday!");
    }
    }
    }

    ReplyDelete
  7. class Div
    {
    public static void main(String args[])
    {
    int d=15;
    String s=((d%3==0)&&(d%5==0))?"Yes":"No";
    System.out.println(s);
    }
    }

    ReplyDelete
  8. class Grt
    {
    public static void main(String args[])
    {
    int x=4,y=1,z=3;
    String s=(x>y&&x>z)?"x is greatest":(y>z)?"y is greatest":"z is greatest";
    System.out.println(s);
    }
    }

    ReplyDelete
  9. class Numalph
    {
    public static void main(String args[])
    {
    char c='4';
    String s=(c>=48&&c<=57)?"Numeric":"Alphabet";
    System.out.println(s);
    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post