A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/operators-in-java/ below:

Java Operators - GeeksforGeeks

Try it on GfG Practice

Java operators are special symbols that perform operations on variables or values. These operators are essential in programming as they allow you to manipulate data efficiently.

Example: Below code describes basic structure of operator. how to use operators like the + and - operators are used to perform addition and subtraction on numeric values."

Java
// Java program to show the use of + and - operators
public class Geeks 
{  
    public static void main(String[] args) 
    {
      // Declare and initialize variables
        int num1 = 500;
        int num2 = 100;
        
        // Using the + (addition) operator
        int sum = num1 + num2;
        System.out.println("The Sum is: "+sum);
        
        // Using the - (subtraction) operator
        int diff = num1 - num2;
        System.out.println("The Difference is: "+diff);
        
    }
}

Output
The Sum is: 600
The Difference is: 400

Explanation:

Types of Operators in Java
  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operator
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. Shift Operators
  9. An instance of an operator

Let's see all these operators one by one with their proper examples.

1. Arithmetic Operators

Arithmetic Operators are used to perform simple arithmetic operations on primitive and non-primitive data types. 

Note:

Example: This example demonstrates the use of arithmetic operators on integers and string-to-integer conversion for performing mathematical operations.

Java
// Java Program to show the use of
// Arithmetic Operators
import java.io.*;

class Geeks 
{
    public static void main (String[] args) 
    {
          
        // Arithmetic operators on integers
        int a = 10;
        int b = 3;
      
        // Arithmetic operators on Strings
        String n1 = "15";
        String n2 = "25";

        // Convert Strings to integers
        int a1 = Integer.parseInt(n1);
        int b1 = Integer.parseInt(n2);
           
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("a / b = " + (a / b));
        System.out.println("a % b = " + (a % b));
        System.out.println("a1 + b1 = " + (a1 + b1)); 
          
    }
}

Output
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
a1 + b1 = 40
2. Unary Operators

Unary Operators need only one operand. They are used to increment, decrement, or negate a value. 

Example: This example demonstrates the use of unary operators for post-increment, pre-increment, post-decrement, and pre-decrement operations.

Java
// Java Program to show the use of
// Unary Operators
import java.io.*;

// Driver Class
class Geeks {
      // main function
    public static void main(String[] args)
    {
        // Interger declared
        int a = 10;
        int b = 10;

        // Using unary operators
        System.out.println("Postincrement : " + (a++));
        System.out.println("Preincrement : " + (++a));

        System.out.println("Postdecrement : " + (b--));
        System.out.println("Predecrement : " + (--b));
    }
}

Output
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
3. Assignment Operator

 '=' The assignment operator is used to assign a value to any variable. It has right-to-left associativity, i.e. value given on the right-hand side of the operator is assigned to the variable on the left, and therefore right-hand side value must be declared before using it or should be a constant. 

The general format of the assignment operator is:

variable = value;

In many cases, the assignment operator can be combined with others to create shorthand compound statements. For example, a += 5 replaces a = a + 5. Common compound operators include:

Example: This example demonstrates the use of various assignment operators, including compound, bitwise, and shift operators, for modifying a variable.

Java
// Java Program to show the use of
// Assignment Operators
import java.io.*;

// Driver Class
class Geeks {
    // Main Function
    public static void main(String[] args)
    {
        
        // Assignment operators
        int f = 7;
        System.out.println("f += 3: " + (f += 3));
        System.out.println("f -= 2: " + (f -= 2));
        System.out.println("f *= 4: " + (f *= 4));
        System.out.println("f /= 3: " + (f /= 3));
        System.out.println("f %= 2: " + (f %= 2));
        System.out.println("f &= 0b1010: " + (f &= 0b1010));
        System.out.println("f |= 0b1100: " + (f |= 0b1100));
        System.out.println("f ^= 0b1010: " + (f ^= 0b1010));
        System.out.println("f <<= 2: " + (f <<= 2));
        System.out.println("f >>= 1: " + (f >>= 1));
        System.out.println("f >>>= 1: " + (f >>>= 1));
    }
}

Output
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
f &= 0b1010: 0
f |= 0b1100: 12
f ^= 0b1010: 6
f <<= 2: 24
f >>= 1: 12
f >>>= 1: 6

Note: Use compound assignments (+=, -=) for cleaner code.

4. Relational Operators

Relational Operators are used to check for relations like equality, greater than, and less than. They return boolean results after the comparison and are extensively used in looping statements as well as conditional if-else statements. The general format is , 

variable relation_operator value

Relational operators compare values and return Boolean results:

Example: This example demonstrates the use of relational operators to compare values and return boolean results.

Java
// Java Program to show the use of
// Relational Operators
import java.io.*;

// Driver Class
class Geeks {
    
    // main function
    public static void main(String[] args)
    {
        // Comparison operators
        int a = 10;
        int b = 3;
        int c = 5;

        System.out.println("a > b: " + (a > b));
        System.out.println("a < b: " + (a < b));
        System.out.println("a >= b: " + (a >= b));
        System.out.println("a <= b: " + (a <= b));
        System.out.println("a == c: " + (a == c));
        System.out.println("a != c: " + (a != c));
    }
}

Output
a > b: true
a < b: false
a >= b: true
a <= b: false
a == c: false
a != c: true
5. Logical Operators

Logical Operators are used to perform "logical AND" and "logical OR" operations, similar to AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the second condition is not evaluated if the first is false.

Conditional operators are:

Example: This example demonstrates the use of logical operators (&&, ||, !) to perform boolean operations.

Java
// Java Program to show the use of
// Logical operators
import java.io.*;

class Geeks {
  
      // Main Function
    public static void main (String[] args) {
      
        // Logical operators
        boolean x = true;
        boolean y = false;
      
        System.out.println("x && y: " + (x && y));
        System.out.println("x || y: " + (x || y));
        System.out.println("!x: " + (!x));
    }
}

Output
x && y: false
x || y: true
!x: false
6. Ternary operator

The Ternary Operator is a shorthand version of the if-else statement. It has three operands and hence the name Ternary. The general format is,

condition ? if true : if false

The above statement means that if the condition evaluates to true, then execute the statements after the '?' else execute the statements after the ':'.  

Example: This example demonstrates the use of the ternary operator to find the maximum of three numbers.

Java
// Java program to illustrate
// max of three numbers using
// ternary operator.
public class Geeks {
  
    public static void main(String[] args)
    {
        int a = 20, b = 10, c = 30, result;

        // result holds max of three
        // numbers
        result = ((a > b) ? (a > c) ? a : c : (b > c) ? b : c);
        System.out.println("Max of three numbers = "+ result);
    }
}

Output
Max of three numbers = 30
7. Bitwise Operators

Bitwise Operators are used to perform the manipulation of individual bits of a number and with any of the integer types. They are used when performing update and query operations of the Binary indexed trees. 

Example: This example demonstrates the use of bitwise operators (&, |, ^, ~, <<, >>, >>>) to perform bit-level operations.

Java
// Java Program to show the use of
// bitwise operators
import java.io.*;

class Geeks 
{
    public static void main(String[] args)
    {
        // Bitwise operators
        int d = 0b1010;
        int e = 0b1100;
      
        System.out.println("d & e : " + (d & e));
        System.out.println("d | e : " + (d | e));
        System.out.println("d ^ e : " + (d ^ e));
        System.out.println("~d : " + (~d));
        System.out.println("d << 2 : " + (d << 2));
        System.out.println("e >> 1 : " + (e >> 1));
        System.out.println("e >>> 1 : " + (e >>> 1));
    }
}

Output
d & e : 8
d | e : 14
d ^ e : 6
~d : -11
d << 2 : 40
e >> 1 : 6
e >>> 1 : 6
8. Shift Operators

Shift Operators are used to shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. They can be used when we have to multiply or divide a number by two. The general format , 

number shift_op number_of_places_to_shift;

Example: This example demonstrates the use of shift operators (<<, >>) to shift the bits of a number left and right.

Java
// Java Program to show the use of
// shift operators
import java.io.*;

class Geeks 
{
    public static void main(String[] args)
    {
        int a = 10;
    
        // Using left shift
        System.out.println("a<<1 : " + (a << 1));
      
        // Using right shift
        System.out.println("a>>1 : " + (a >> 1));
    }
}

Output
a<<1 : 20
a>>1 : 5
9. instanceof Operator

The instanceof operator is used for type checking. It can be used to test if an object is an instance of a class, a subclass, or an interface. The general format,  

object instance of class/subclass/interface

Example: This example demonstrates the use of the instanceof operator to check if an object is an instance of a specific class or interface

Java
// Java program to show the use of
// Instance of operator
public class Geeks 
{
    public static void main(String[] args)
    {

        Person obj1 = new Person();
        Person obj2 = new Boy();

        // As obj is of type person, it is not an
        // instance of Boy or interface
        System.out.println("obj1 instanceof Person: "
                           + (obj1 instanceof Person));
        System.out.println("obj1 instanceof Boy: "
                           + (obj1 instanceof Boy));
        System.out.println("obj1 instanceof MyInterface: "
                           + (obj1 instanceof MyInterface));

        // Since obj2 is of type boy,
        // whose parent class is person
        // and it implements the interface Myinterface
        // it is instance of all of these classes
        System.out.println("obj2 instanceof Person: "
                           + (obj2 instanceof Person));
        System.out.println("obj2 instanceof Boy: "
                           + (obj2 instanceof Boy));
        System.out.println("obj2 instanceof MyInterface: "
                           + (obj2 instanceof MyInterface));
    }
}

// Classes and Interfaces used
// are declared here
class Person {
}

class Boy extends Person implements MyInterface {
}

interface MyInterface {
}

Output
obj1 instanceof Person: true
obj1 instanceof Boy: false
obj1 instanceof MyInterface: false
obj2 instanceof Person: true
obj2 instanceof Boy: true
obj2 instanceof MyInterface: true
Common Mistakes to Avoid

The common mistakes that can occur when working with Java Operators are listed below:

Operator Precedence and Associativity in Java



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4