Recent Posts

header ads

What is Operators in java with examples । Types of Operators। KpTechSolution

Today we will know what are the operators in Java? And what does it matter? Operators in Java are a type of Symboles that perform operations in Java Source Code such as adding, subtracting, multiplying, parting etc. All this can be possible only through operators in Java. Operators in Java have been placed in many different categories based on their work. By using which programmers can perform Operation Performers in their Source Program and by using operators accordingly.

Operators in java - types of Operators - kptechsolution.com

In order to perform various types of operation in Java Programming Language, we use different types of operators. Meaning the operation between the data and the variables is performed in the program. This work is possible only through operators and only. Therefore operators have an important contribution in Java.

Programmers use operators to perform operations in Java These operators are divided into several parts, which is very important to know the programmer. If he wants to calculate something in his program, he uses Java Operators. So friends know today how many types of operators in Java are used for this work and all these things today we will learn through example.

Do not think that operators in Java Programming Language are used only for addition, subtraction, multiplication and division, but operators are also used for any type of mathematical calculation and logical calculation too.

So let us now know about the operators used in Java Programming Language in detail.

➧Types of Operators :

Operators in Java Programming Language are divided into the following 6 types: These are 6 types of operators -
  1. Arithmetic Operators.
  2. Logical Operators.
  3. Assignment Operators.
  4. Bitwise Operators.
  5. Relational Operators.
  6. Increment / Decrement Operators.
Let us explore the 6 types of operators in the Java Programming Language in detail, because they are very important for the programmer to know them in detail. Friends, if you have not understood right about operators, then let us understand all the operators by example. In Java Programming Language, there are 6 types of operators:

1.Arithmetic Operators :

In the Java programming language, arithmetic operators are all operators that are used to calculate all the mathematical calculations or any logical calculations.

In simple words, if you are asked to do calculation using any number of operators in the question of mathematics, then all operators are called semantic operators. So let's also know their names, they have the following names:

Arithmetic Operators in  Java  :
 Operators             Examples
Addition ( + )                 a + b
Subtraction ( + )             a – b
Multiplecations  ( * )       a * b
Division  ( / )                  a / b
Modulus  ( % )                 a % b

Java Source  Code :
  1. Import java.util.Scanner.*;
    class Arithmetic
    {
    private static Scanner s;
    public static void main (String args [] )
    int addition, subtraction, multiplication, division, modulus, x, y ;
    s = new Scanner (System.in );
    System.out.println (“\n Enter Two Number For Calculation : “);
    x = s.nextInt();
    y = s.nextInt();
    addition = x + y ;
    subtraction = x – y ;
    multiplication = x * y;
    division = x / y;
    modulus = x % y;
    System.out.println ( “Addition : “+addition );
    System.out.printl ( “Subtraction : “+subtraction );
    System.out.println ( “Multiplication : “+multiplication );
    System.out.println ( “Devirsion : “+division );
    System.out.println ( “Modulus : “+modulus );
    }
    }

2.Logical Operators :

In Java Programming Language today, we will have to do logical operations. Logical operation is used to perform operations on a combination of 2 Conditions.

There are three types of logical operators in the Java programming language -
  1. Logical AND  ( && ).
  2. Logical OR ( || ).
  3. Logical NOT ( ! ).
I. Logical AND ( && ) : Suppose we have two variables a and b. For two variables, we are going to perform logical operations. If we type a = true and b = false then we get false in the result. If we type a = false and b = true, then we get false in the result. If we type a = true and b = true then we get true in the result. And if we write a = false and b = false, then we get false in the result.

Truth Table : 
A                     B                   A && B 
True             False             False
False            True              False
False            False             False
True             True              True

II. Logical OR ( || ) : Suppose we have two variables a and b. For two variables, we are going to perform logical operations. If we type a = true and b = false then we get true in the result. If we type a = false and b = true, then we get true in the result. If we type a = true and b = true then we get true in the result. And if we write a = false and b = false, then we get false in the result.

Truth Table : 
A                     B                   A && B 
True            False             True
False           True              True
True            True              True
False           False             False

III. Logical NOT ( ! ) : Suppose a variable is x. If we do asign (x = true) in it then! X gets false in the result, and if we do asx (x = false) then we get true in the result by x. This means that when we do asign (True or False) in the variables that do NOT (!) Operation Perform, we get the opposite result.

Java Source Code :
  1. Import java.util.Scanner.*;
  2. public class Logical
  3. {
  4. private static Scanner s;
  5. public static void main (String args [] )
  6. int age ;
  7. s = new Scanner (System.in );
  8. System.out.println (“\n Enter Your Age : “);
  9. age = s.nextInt();

  10. if ( !( age >16 ) )
  11. {
  12. System.out.println ( “You are Not eligible you are chiled “ );
  13. }
  14. else if ( age >16 && age < 36 )
  15. {
  16. System.out.printl ( “You are young man “);
  17. }
  18. else if ( age == 36 || age <= 60 )
  19. {
  20. System.out.println ( “your age is middle” );
  21. }
  22. else
  23. {
  24. System.out.println ( “You are old Man” );
  25. }
  26. }

    3.Assignment Operators :

    Like Assignment itself, it is known that its work is to Asign Value in Variables. Assignment Operators stores Value in Variables, it is called Value asign.

    Let's assume that in the variable A, 10 is to Asign and in the variable b, 20 is to Asign, then it is possible only by an assignment operator, so let us understand it well by using Examples.

    Assignment Operators in Java :

    Operators           Examples
      =             a = 5, b = 10 (10 in a and b has 10 asign)
     +=            x + = a (this means x = x + a)
     -=             x - = a (this means x = x - a)
     *=             x * = a (this means x = x * a)
     /=             x / = a (this means x = x / a)
     %=             x% = a (this means x = x% a)

    Java Source Code :
    1. class Arithmetic
      {
      public static void main (String args [] )
      {
      int a = 10 ;
      a+=5 ;
      System.out.println ( “Addition : “+a ) ;
      a -= 5 ;
      System.out.printl ( “Subtraction : “+a );
      a *= 5 ;
      System.out.println ( “Multiplication : “+a );
      a /= 5 ;
      System.out.println ( “Devirsion : “+a );
      a %= 5 ;
      System.out.println ( “Modulus : “+a );
      }
      }

    4. Bitwise Operators :

    Bitwise operators are used to perform bit operations. In this, all the decimal values are converted to Binary Value. We work on this same Binary Numbers. Let's try to understand Bitwise operators through an example. Below are examples of Bitwise Operators in Java Programming Language -

    Bitwise Operators in Java : 

    Operators       Meaning                            Use     
          &               Bitwise AND                     a & b
           |               Bitwise OR                       a | b
           ^               Bitwise exclusive OR        a ^ b
           ~               Bitwise Complement         ~a
          <<              Shift Left                         a << 1
          >>              Shift Right                        a >> 1

    Truth Table :
        X             Y            X & Y           X | Y
        0             0               0                  0
        0             1               0                  1
        1             0               0                  1
         1               1                 1                    1

    5. Relational Operators :

    As the name suggests, Relational operators are used to detect the relationship between 2 variables. It shows relation between 2 variables. And if the relationship is correct 1 returns and if the relationship is wrong then 0 returns.

    The relational operator is used more for Conditions and Loop. In the next lesson, we will read about Conditions and Loop. Now you guys have no need to think about it. So let's understand the relational operator by example.

    Relational Operators in Java :

    Operators             Examples
         >                 a> b (a big is b)
         <                 a <b (a is smaller than b)
        >=                a> = b (a is equal to b or is greater)
        <=                a <= b (a is equal to b or small)
        ==                a == b (a is equal to b)
        !=                a! = b (a and b are not equal)

    Java Source Code :
    1. Import java.util.Scanner.*;
      class Relation
      {
      private static Scanner s;
      public static void main (String args [] )
      {
      int x, y ;
      s = new Scanner (System.in );
      System.out.println ("\n Enter Two Number For Comparison s : ");
      x = s.nextInt();
      y = s.nextInt();
      System.out.println ( “ Result of a > b:“+(a>b) );
      System.out.printl ( “Result of a < b :”+(a<b) );
      System.out.println ( “Result of a >= b : “+( a >= b) );
      System.out.println ( “Result of a <= b : “+( a <= b));
      System.out.println ( “Result of a == b : “+ ( a == b) );
      System.out.println ( “Result of a != b : “+ ( a != b) );
      }
      }

    6. Increment / Decrement Operators :

    In the Java Programming Language, there are increment / decrement which are used to increase and decrease their value. It is used in two ways. The following are two types:

    1. ++ increment operator : 

    This increment operator adds 1 to its value as if value is 3 then ++ will be 3 ++ = 4 after the increment operator is performed.

    2. -- decrement operator :

    This decrement operator reduces its value to 1 as if value is 3 then - after the decrement operator is performed, it will be 3-- = 2.

    Java Source Code :
    1. Import java.util.Scanner.*;
      public  class Sample
      {
      private static Scanner s;
      public static void main (String args [] )
      int  n;
      s = new Scanner (System.in );
      System.out.println (“\n Please Enter Your Number : “);
      n = s.nextInt();
      System.out.println ( “ ---- Increment / Decrement Examples For You ----");
      System.out.printl ( “Value of n is : %d \n” , n);
      System.out.println ( “Value of n is : %d \n” , n++);
      System.out.println ( “Value of n is : %d \n” , n--);
      }
      }

    So friends, I hope you guys have understood the operators very well. But even if you have a question, then you can ask me to comment and I will definitely answer your question. Thanks!

    Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.

    Thank You !

    Note - Fore more Topics are available in www.kptechsolution.com. 

    Post a Comment

    0 Comments