Java Operators

Operators in Java

Operators are used to perform mathematical, Comparison and Logical operations.

Important categories of Operators

a) Assignment Operators

b) Arithmetic operators

c) Relational Operators

d) Logical operators

etc...
-----------------------
a) Assignment Operators

1) Assignment operator =

a = 10;

2) Add and +=

a = 10;

a += 20;

3) Subtract and Assign -=

a = 10;

a -= 5;

4) Multiply and Assign *=

a = 10;

a *= 5;
------------------------

Example:
public static void main (String [] args){
        int a = 10;
        System.out.println (a); // 10
        a += 10;
        System.out.println (a); // 20
        a -= 10;
        System.out.println (a); // 10
        a *= 5;
        System.out.println (a); //50
    }
--------------------------------------
b) Arithmetic operators

1) Addition + (for addition and string concatenation)

2) Subtraction - (fro subtraction and negation)

3) Multiplication *

4) Division /

5) Modules %

6) Increment ++

7) Decrement --
-----------------------------
Example:
public static void main (String [] args){
        int a= 10, b = 5;
        String c ="Selenium", d = "Testing";
      
    System.out.println ("Addition of a, b is: " + (a+b)); // 15
    System.out.println ("Concatenation of c, d is: " + (c+d)); // SeleniumTesting
  
    System.out.println ("Subtraction of a, b is: "+ (a-b)); //5
    System.out.println ("Multiplication of a, b is: "+(a * b)); //50
    System.out.println ("Division of a, b is: "+ (a/b)); //2
    System.out.println ("Modulus of a, b is: "+ (a%b)); //0
    b = 5;
    a = ++b;
    System.out.println (a); // 5
    b = 5;
    a = --b;
    System.out.println (a); //4
    b = 5;
    a = b+4;
    System.out.println (a); // 9
  
    }
--------------------------------------
c) Relational operators
Types of Result in Computer Programming

i) Value based Result

3 + 5 = 8

2 * 7 = 14

ii) Boolean / Logical Result

True or false

iii) Constant based Result
-------------------------------------
Relational operators return Boolean / Logical result

1) ==

2) !=

3) >

4) >=

5) <

6) <=
----------------------
Example:

public static void main (String [] args){
        int a=10, b=20;
        System.out.println ("a > b is "+ (a > b)); //False
        System.out.println ("a >= b is "+ (a >= b)); //False
        System.out.println ("a == b is "+ (a == b)); //False
      
        System.out.println ("a < b is "+ (a < b)); //True
        System.out.println ("a <= b is "+ (a <= b)); //True
        System.out.println ("a != b is "+ (a != b)); //True
        }
----------------------------
d) Logical Operators

1) Logical Not operator !

2) Logical And operator &&

3) Logical Or Operator ||
------------
Result Criteria

1) Not operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true        true        false
true        false        true
false        true        true
false        false        true
-----------------------------------      
2) And operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true        true        true
true        false        false
false        true        false
false        false        false
---------------------------------------
3) Or operator
----------------------------
Operand 1    Operand 2    Result
-------------------------------------
true        true        true
true        false        true
false        true        true
false        false        false
------------------------------------
Example:

public static void main (String [] args){
        boolean a= true, b=false;
        System.out.println("! (a && b) is: " + ! (a && b )); //True
        System.out.println("(a && b) is: " + (a && b )); //False
        System.out.println("(a || b) is: " + (a || b )); //True
        }
--------------------------------------------  
Example 2:
public static void main (String [] args){
        int a = 100, b = 500, c = 70;
      
        if ((a > b) && (a > c)) {
            System.out.println ("A is a Big Number");
        }
        else
        {
            System.out.println ("A is Not a Big Number");
        }
----------------------------------------------

0 comments:

Post a Comment