Java | Operators | Tutorial 3

0

 


Programming Language: Java

Topic: Operators

Video: 3


_________________________________________________________________________________________

Operators

Operators are used to perform operations on variables. These operators are very useful in almost all programs.

The different types of operators are:
  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators

Arithmetic Operators

Arithmetic Operators are the operators which are used to perform arithmetical/mathematical operations. 

Operator

Name

Description

Example

+

Addition

Adds together two values

x + y

-

Subtraction

Subtracts one value from another

x - y

*

Multiplication

Multiplies two values

x * y

/

Division

Divides one value by another

x / y

%

Modulus

Returns the division remainder

x % y

++

Increment

Increases the value of a variable by 1

++x

--

Decrement

Decreases the value of a variable by 1

--x


Assignment Operator

Assignment operator is used to assign a value to a variable.

Operator

Example

Same As

=

x = 5

x = 5

+=

x += 3

x = x + 3

-=

x -= 3

x = x - 3

*=

x *= 3

x = x * 3

/=

x /= 3

x = x / 3

%=

x %= 3

x = x % 3

&=

x &= 3

x = x & 3

|=

x |= 3

x = x | 3

^=

x ^= 3

x = x ^ 3

>>=

x >>= 3

x = x >> 3

<<=

x <<= 3

x = x << 3



Comparison Operator

Comparison Operators are used to compare two variables or values and returns answer in boolean; i.e. True or False

Operator

Name

Example

==

Equal to

x == y

!=

Not equal

x != y

> 

Greater than

x > y

< 

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y



Logical Operators

Logical operators are used to determine the logic between the variables or values. 

Operator

Name

Description

Example

&& 

Logical and

Returns true if both statements are true

x < 5 &&  x < 10

|| 

Logical or

Returns true if one of the statements is true

x < 5 || x < 4

!

Logical not

Reverse the result, returns false if the result is true

!(x < 5 && x < 10)



Code File: (Operators.java)

public class Operators {
    public static void main(String[] args) {
        int num1 = 12;
        float num2 = 9;

        // Add
        System.out.println(num1 + num2);
        // Sub
        System.out.println(num1 - num2);
        // multiplication
        System.out.println(num1 * num2);
        // division
        System.out.println(num1 / num2);

        // Equal Comparision
        System.out.println(num1 == num2);
        // Not Equal
        System.out.println(num1 != num2);
        // Greater than
        System.out.println(num1 > num2);
        // Less Than
        System.out.println(num1 < num2);
        // Greater than Equal to
        System.out.println(num1 >= num2);
        // Less Than Equal to
        System.out.println(num1 <= num2);
    }
}

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !