Operators in
Java
Java can be used
to do both basic and advanced operations and to perform these functions, operands
are used. Associativity represents the order in which code starts compiling.
Eg) 11+11=22
Here, the
associativity is from left to right which means that ‘11’ is added with ‘11’
and assigned to the right side. Precedence refers to the priority given and
more the precedence value, better is the order of priority.
Eg) Brackets (( ),
[ ]) are given the highest precedence value 15 and hence it executes first.
Operators
with Precedence 1
|
Precedence |
Operator |
Type |
Associativity |
|
1 |
= |
Assignment |
Right to Left |
|
+= |
Addition
Assignment |
||
|
-= |
Subtraction
Assignment |
||
|
*= |
Multiplication
Assignment |
||
|
/= |
Division
Assignment |
||
|
%= |
Modulus
Assignment |
byte
n0_1=22;
In the above statement, no_1 is assigned with the number 22, using the assignment operator. Suppose you want to add 11 with no_1 and store it again in number no_1, you can use the following command:
no_1=no_1+11;
The above command also has a shortcut, which is obtained by ‘+=’ assignment.
no_1+=11;
Similarly, all the other assignment operators function in the similar manner, and is evident from the following code.
Java code using operators with precedence 1
class operator{
public static void main(String args[]){
int no_1=1129;
// Adds 7 with no_1 and stores in no_1
no_1+=7;
System.out.println(no_1);
// Subtracts 7 with no_1 and stores in no_1
no_1-=7;
System.out.println(no_1);
// Multiplies 7 with no_1 and stores in no_1
no_1*=7;
System.out.println(no_1);
// Divides 7 with no_1 and stores in no_1
no_1/=7;
System.out.println(no_1);
//Performs modulus operation with no_1 and stores in no_1
//Outputs the reminder
no_1%=7;
System.out.println(no_1);
}
}
Output
Operator with precedence 2
|
Precedence |
Operator |
Type |
Associativity |
|
2 |
?: |
Ternary
conditional |
Right to Left |
The basic format of the ternary conditional operator is:
Condition ? "true" : " false"This means when a given condition is true, the statement after ‘?’ gets executed whereas, when the condition is false, the statement after ‘:’ gets executed.
Java program
to check whether students pass in an exam
Eg) Consider 2 students write an exam and they will pass only if their score is greater than 40. The following program can be used to check the condition whether they passed or failed in the exam.
class operator{
public static void main(String args[]){
//Assigning student 1 mark
byte marksOfStudent_1=77;
System.out.print("student 1 ");
//Printing whether student 1 passed or failed in the exam
String output_1= marksOfStudent_1 >=40 ? "pass" : "fail";
System.out.println(output_1);
//Details of student 2
System.out.print("student 2 ");
byte marksOfStudent_2=22;
String output_2= marksOfStudent_2 >=40 ? "pass" : "fail";
System.out.println(output_2);
}
}Output
In the above code, student 1 is assigned with 77 marks. Since it is greater than 40, the condition is satisfied and hence ‘true’ is printed in the output. Student 2 is assigned with 22 marks and since it is lesser than 40, false is printed in the output.
To learn more about Java operators, you can check these useful links:




0 Comments