Home > Blog > What are Java operators: Types & Examples

What are Java operators: Types & Examples

What are Java operators: Types & Examples

By Upskill Campus
Published Date:   31st January, 2023 Uploaded By:    Satyanshu Pathak
Table of Contents [show]

Introduction

 

Java is a popular object-oriented programming language that’s used for creating web applications and other software. Operators in Java are special symbols that allow you to perform certain operations on data. These operators can be used to manipulate variables and constants, perform mathematical functions, assign values to variables, compare values, and even control the flow of your program. If you’re new to coding in Java or just need a refresher on its operators, this blog post is for you! We’ll discuss what operators in Java are and how they work, as well as provide some examples of how to use them. Let’s dive right into it!

 

Types of Java Operators

 

In Java, different types of operators can be used to perform operations on variables and values. These operators are classified into various categories, like arithmetic operators, comparison operators, assignment operators, bitwise operators, logical operators, etc.

 

Arithmetic Operators

 

Arithmetic operators are used to performing arithmetic on two operands at the same time. The four basic arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

 

The addition operator adds two operands together and produces the sum. For example, 4 + 3 produces 7. The subtraction operator subtracts the second operand from the first and produces the difference. For example, 10 - 5 produces 5.

 

The multiplication operator multiplies two operands together and produces the product. For instance, 3 * 4 equals 12. The division operator divides the first operand by the second and produces the quotient. For example, 10 / 5 produces 2.

 

The remainder of a division operation is returned by the modulus operator. For example, 10 % 3 produces 1 because 3 goes into 10 three times, leaving a remainder of 1.

 

Assignment Operators

 

To assign values to variables in Java, utilise the assignment operators. The = operator is the most basic assignment operator and is used to assign a value to a variable. For instance, the following code gives the variable x the value 20:

 

x = 20;

 

Other assignment operators are used to performing operations on a variable and then assign the result to that variable. For example, the += operator adds a value to a variable and then assigns the result to that variable. So, the following code would add 10 to the current value of x and then assign the result (30) back to x:

 

x += 10;

 

There are several other assignment operators in Java, including -=, *=, /=, and %=. These all work in a similar way to +=, just with different operations. Assignment operators can be used with any type of data, including numbers, strings, characters, and objects.

 

Comparison Operators

 

Comparison operators are used to comparing two values or variables. The result of a comparison operator is always a Boolean value (true or false). Some of the most commonly used comparison operators in Java are:

 

> (greater than)

< (less than)

== (equal to)

!= (not equal to)

>= (greater than or equal to)

<= (less than or equal to)

 

Logical Operators

 

The logical operators are used to test for true or false conditions. They are typically used with boolean variables (variables that can only hold the value true or false).

 

There are three logical operators in Java:

 

The AND operator (&&)

The OR operator (||)

The NOT operator (!)

 

The AND operator (&&)

 

The AND operator is a binary operator, meaning it takes two operands. It returns true if both operands are true and false if either operand is false. For example:

 

true && true // returns true 

true && false // returns false 

false && true // returns false

 

Bitwise Operators

 

Bitwise operators are used to performing operations on individual bits of an integer. These operators can be used to manipulate the data more efficiently than the logical operators.

 

The bitwise AND operator (&) sets the corresponding bit to 1 if both operands have that bit set to 1. Otherwise, it sets the corresponding bit to 0.

 

The bitwise OR operator (|) sets the corresponding bit to 1 if either operand has that bit set to 1. Otherwise, it sets the corresponding bit to 0.

 

The bitwise XOR operator (^) sets the corresponding bit to 1 if one operand has that bit set to 1 and the other operand has that bit set to 0. Otherwise, it sets the corresponding bit to 0.

 

The left shift operator (<>) moves all bits in its first operand left by a certain number of bits, filling in with zeros from the right side. The second operand specifies how many places to move the bits.

 

The signed right shift operator (>>) moves all bits in its first operand right by a certain number of bits, filling in with zeros from the left side. The second operand specifies how many places to move the bits. This operator preserves the sign of its first operand; therefore, it is called a signed right shift operator.

 

The unsigned right shift operator (>>>) moves all bits in its first operand right by a certain number of bits.

 

Conditional Operator

 

The Java conditional operator is a ternary operator that is used to evaluate boolean expressions. The operator can be used as either an infix or a prefix operator. The following is the syntax for the conditional operator is:

 

condition? expression1 : expression2

 

The condition must be a boolean expression. If the condition is true, then the first expression is evaluated. The second expression is evaluated if the condition is false. The value of whichever expression is evaluated is the result of the conditional operator.

 

For example, consider the following code:

 

int x = 20; int y = 15; int z = (x > y) ? x : y;

 

In this code, the variable z will be assigned the value of x if x is greater than y. If x is not greater than y, then z will be assigned the value of y.

 

Type Comparison Operator (instanceof)

 

The instanceof operator is a type comparison operator that compares an object to a specified type. If the object is of the provided type, it returns true. Otherwise, it returns false.

 

The instanceof operator can be used to compare a superclass reference variable to a subclass object. For example, given the following class hierarchy:

 

Object

/ \

Animal Cat

 

The following code would return true:

 

Animal animal = new Cat(); 

System.out.println(animal instanceof Cat); //true 

System.out.println(animal instanceof Animal); //true 

System.out.println(animal instanceof Object); //true

 

Precedence and Associativity of Java Operators

 

Precedence and associativity are two important concepts that determine the order in which operators are evaluated in an expression. Operator precedence determines the order in which operators are applied, while associativity determines whether an operator is applied from left to right or from right to left.

 

Java has well-defined precedence and associativity for all its operators. The following table lists all the operators from highest to lowest precedence, and their associativity:

 

Operator Associativity

() [] . (dot operator) Left to right

! ~ ++ -- + (unary) Right to left

* / % Left to right

+ - Left to right

<< >> >>> Left to right

< > <= >= instanceof Left to right

== != Left to right & (bitwise AND) Left to right ^ (bitwise exclusive OR) Left to right | (bitwise inclusive OR) Left to right && (logical AND) Left to right || (logical OR) Left to right ? : Right to left = += -= *= /= %= &= ^= |= <<= >>= >>>= Right to left

 

Conclusion

 

To learn more about Java Programming Language stay connected with us. We hope this article has introduced Java operators and shown how they can be used in programming. Learning to use the different types of operators is essential for mastering the language so that you can write efficient code. As with any skill, practice makes perfect, so make sure to remember these tips and try them out on your projects. With enough practice, you'll soon master all of the various types of Java operators!

About the Author

Upskill Campus

UpskillCampus provides career assistance facilities not only with their courses but with their applications from Salary builder to Career assistance, they also help School students with what an individual needs to opt for a better career.

Recent Blog

Leave a comment