Training Of Java Operators (In Quite Simple Language)
Operators are special symbols (characters) that perform operations on operands (variables and values). For example, the + operator is a plural.
In this tutorial, you will learn everything about the different types of operators in the Java programming language, how to use them by example.
Assignment Operator
Assignment operators in Java are used to assign values to variables. For example
int age;
age = 5;
The assignment operator assigns its right value to the variable on its left. Here, 5 is assigned to the age variable using the = operator.
There are other assignment operators. However, for convenience, we will train other assignment operators later.
Example 1: Assignment operator
- class AssignmentOperator {
- public static void main (String [] args) {
- int number1, number2;
- // Assigning 5 to number1
- number1 = 5;
- System.out.println (number1);
- // Assigning value of variable number2 to number1
- number2 = number1;
- System.out.println (number2);
- }
- }
Output
5
5
Mathematical Operators
Mathematical operators are used to perform mathematical operations such as addition, subtraction, multiplication, and so on.
Example 2: Mathematical operator
- class ArithmeticOperator {
- public static void main (String [] args) {
- double number1 = 12.5, number2 = 3.5, result;
- // Using addition operator
- result = number1 + number2;
- System.out.println (“number1 + number2 =” + result);
- // Using subtraction operator
- result = number1 – number2;
- System.out.println (“number1 – number2 =” + result);
- // Using multiplication operator
- result = number1 * number2;
- System.out.println (“number1 * number2 =” + result);
- // Using division operator
- result = number1 / number2;
- System.out.println (“number1 / number2 =” + result);
- // Using remainder operator
- result = number1% number2;
- System.out.println (“number1% number2 =” + result);
- }
- }
Output
number1 + number2 = 16.0
number1 – number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1% number2 = 2.0
In the example above, all operands used are variable. Numbers can also be used, they do not have to be variable.
result = number1 + 5.2;
result = 2.3 + 4.5;
number2 = number1 -2.9;
The + operator can also be used to join two or more strings.
Example 3: Mathematical operator
- class ArithmeticOperator {
- public static void main (String [] args) {
- String start, middle, end, result;
- start = “Talk is cheap. “;
- middle = “Show me the code. “;
- end = “- Linus Torvalds”;
- result = start + middle + end;
- System.out.println (result);
- }
- }
Output
Talk is cheap. Show me the code. – Linus Torvalds
Unit operators
Operations are performed on only one operand.
Example 4: Unit operator
- class UnaryOperator {
- public static void main (String [] args) {
- double number = 5.2, resultNumber;
- boolean flag = false;
- System.out.println (“+ number =” + + number);
- // number is equal to 5.2 here.
- System.out.println (“- number =” + -number);
- // number is equal to 5.2 here.
- // ++ number is equivalent to number = number + 1
- System.out.println (“number =” + ++ number);
- // number is equal to 6.2 here.
- // – number is equivalent to number = number – 1
- System.out.println (“number =” + –number);
- // number is equal to 5.2 here.
- System.out.println (“! Flag =” +! Flag);
- // flag is still false.
- }
- }
Output
+ number = 5.2
-number = -5.2
number = 6.2
number = 5.2
! flag = true
You can also use ++ and – as prefixes and extensions in Java. The ++ operator increases the value by 1 unit and – decreases the value by 1 unit.
int myInt = 5;
++ myInt // myInt becomes 6
myInt ++ // myInt becomes 7
–MyInt // myInt becomes 6
myInt– // myInt becomes 5
There is a fundamental difference when using incremental and decrement operators as prefixes and suffixes. Consider the following example,
- class UnaryOperator {
- public static void main (String [] args) {
- double number = 5.2;
- System.out.println (number ++);
- System.out.println (number);
- System.out.println (++ number);
- System.out.println (number);
- }
- }
Output
5.2
6.2
7.2
7.2
When ordered
System.out.println (number ++)
Runs, first prints the original value and then increments one unit. That’s why the output is 5.2. Then, when
System.out.println (number)
Runs, prints 6.2.
But, when
System.out.println (++ number)
Runs, before printing on the screen, its value increases by 1 unit.
For – the same is true.
Equality and relational operators
Equality and relational operators determine the relationship between two operators. Checks that one operand is larger, smaller, equal, unequal, and از larger than another. Depending on the relationship, the result is right or wrong.
Equality and relational operators are used in decision making and loops.
Example 6: Equality and relational operators
- class RelationalOperator {
- public static void main (String [] args) {
- int number1 = 5, number2 = 6;
- if (number1> number2)
- {
- System.out.println (“number1 is greater than number2.”);
- }
- else
- {
- System.out.println (“number2 is greater than number1.”);
- }
- }
- }
Output
number2 is greater than number1.
Here, we used <to check if number1 is greater than number2.
Because number2 is greater than number1, the expression number1> number2 is considered incorrect.
Hence, the else part code is executed and if the condition is true, the code inside the if body is executed.
Remember that equality and relational operators compare two operators and evaluate them as true or false.
In addition to relational operators, there is an example comparative operator that compares an instance of an object with a specific type.
The instanceof operator
Here is an example of an instanceof operator.
- class instanceofOperator {
- public static void main (String [] args) {
- String test = “asdf”;
- boolean result;
- result = instanceof test String;
- System.out.println (result);
- }
- }
When you run the program, the output will be true. This is because test is an example of a String class.
Logical operators
Logical Operators || (Conditional-OR) and && (Conditional-AND) work with Boolean expressions.
Example 8: Logical operator
- class LogicalOperator {
- public static void main (String [] args) {
- int number1 = 1, number2 = 2, number3 = 9;
- boolean result;
- // At least one expression needs to be true for result to be true
- result = (number1> number2) || (number3> number1);
- // result will be true because (number1> number2) is true
- System.out.println (result);
- // All expression must be true from result to be true
- result = (number1> number2) && (number3> number1);
- // result will be false because (number3> number1) is false
- System.out.println (result);
- }
- }
Output
true
false
Logical operators are used in decision making and looping.
Triple operators
Conditional operator or triple operator?: Is an abbreviated if-else structure. The syntax of the conditional operator is:
variable = Expression? expression1: expression2
Here’s how it works.
- If Expression is true, the value of expression1 is assigned to the variable.
- If Expression is false, the value of expression2 is assigned to the variable.
Example 9: Triple operator
- class ConditionalOperator {
- public static void main (String [] args) {
- int februaryDays = 29;
- String result;
- result = (februaryDays == 28)? “Not a leap year”: “Leap year”;
- System.out.println (result);
- }
- }
Output
Leap year
Bitwise and Bit Shift operators
These operators are used to perform bit operations and bit shifts in Java.
These operators are not commonly used.
Other assignment operators
We only talked about one assignment operator = at the beginning of the tutorial. In addition to this operator, there are other assignment operators that help us write cleaner code.