Java Operators and Assignments
What are operators and its types in java ?
Operator in Java is a symbol which is used to perform operations on one, two, or three operands, and then return a result.

Operators in Java can be classified into 6 types:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are the symbols that represent arithmetic math operations.
Types of Arithmetic Operators in java :

Example of Arithmetic Operators

Output

2. Logical Operator.
- Logical operators verify whether the given expression is true or false.
- They are used in decision making.

Example of Logical Operators

Output :

3.Relational Operators
In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities.

Relational operators are used to check the relationship between two operands. For example,
// check is a is less than b a < b; Here, < operator is the relational operator. It checks if a is less than b or not.
It returns either true or false.
Example of Relational Operators :

Output :

Note: Relational operators are used in decision making and loops.
4.InstanceOf Operator
The instanceof operator checks whether an object is an instanceof a particular class. It returns a Boolean value.
Example of InstanceOf Operators :

Output

5.Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example,
variable = condition ? expression1 : expression2
Here’s how it works.
- If the condition is true, expression1 is assigned to the variable.
- If the condition is false, expression2 is assigned to the variable.
Example Of Ternary Operator

Output

6. Bitwise Operator



Examples Of Bitwise Operator
1. Bitwise AND

2. Bitwise Exclusive OR(^)

3. Bitwise Inclusive OR(|)

4. Bitwise Compliment

Output :
1.

2.

3.

4.

7. Unary Operator
Java unary operators are the types that need only one operand to perform any operation

Examples of Unary Operator:
1. Unary Plus and Minus.

2. Increment and Decrement

3. Logical NOT

Output:
1.

2.

3.

Operator Precedence
- Operator precedence determines the order in which the operators in an expression are evaluated.
- Now, look at the statement below:
- int myInt = 12–4 * 2;
- What will be the value of myInt? Will it be (12–4)*2, that is, 16? Or it will be 12 — (4 * 2), that is, 4?
- When two operators share a common operand, 4 in this case, the operator with the highest precedence is operated first.
- In Java, the precedence of * is higher than that of -. Hence, the multiplication is performed before subtraction, and the value of myInt will be 4.
Precedence table
The table below lists the precedence of operators in Java; higher it appears in the table, the higher its precedence.


Example: Operator Precedence
class Precedence {
public static void main(String[] args) {
int a = 10, b = 5, c = 1, result;
result = a-++c-++b;
System.out.println(result);
}}
Output:
2
The operator precedence of prefix ++ is higher than that of — subtraction operator. Hence, result = a-++c-++b;
is equivalent to result = a-(++c)-(++b);
Thus, when dealing with multiple operators and operands in a single expression, you can use parentheses like in the above example for clarity. The expression inside the parentheses is evaluated first.
Associativity of Operators in Java

Order of Evaluation in Java vs C++
- Java specifies more about the order of evaluation of expressions than C++.
- C++ states undefined behavior if any of the legal evaluation orders of your expression modify an object twice between sequence points.
- i++ + i++ is well defined in Java but has undefined behavior in C++.
- Therefore, Java to C++ code changing cannot be just done syntactically. Logical changes also need to be done.
Why can’t we overload operators in java
- Java does not support operator overloading due to the following reasons −
- Makes code complex − In case of operator overloading the compiler and interpreter (JVM) in Java need to put an extra effort to know the actual functionality of the operator used in a statement.
- Programming error − Custom definition for the operators creates confusion for the programmers, especially the new developers. Moreover, while working with programming languages that support operator overloading the program error rate is high compared to others.
- Easy to develop tools like IDEs − Removal of operator overloading concept keeps the language simple for handling and process leading to a number of Integrated development environment in Java.
- Method overloading − The functionality of operator overloading can be achieved in Java using method overloading in Java in a simple, error free and clear manner.
Example:

Output:

Example:

Output:

Conclusion
We can clearly see that Operators and Assignments have a very big and important role in Java programming.
Contributors
Vidhisha Kamat
Rohan Katkar
Rohan Katta
Kushal Kela
Aayush Khandekar