Java Basic Knowledge (3 - 2) : Other Basic Mathematical Operation

Welcome to MIL.System. This is second edition of my post about Basic Mathematical Operation. After we talk about Arithmetic Operators, Relational Operators, Bitwise Operators, Logical Operators, and Assignment Operators, now we are going to talk the "others" of operators. What is "others" and what are they?. Let's start.

Others or .etc operators are few other operators that supported by Java language. 

Conditional Operator ( ? : )

Also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean Expression. The goal is to decide which value should be assigned to the variable. The operator is written as:

variable X = (expression) ? value if true : value if false

Example


This will produce following result:
Output

instanceof Operator

This operator is used only for object reference variables. The operators check whether the object is of a particular type (class type or interface type). instanceof operator is written as:

( object reference variable ) instanceof (class/interface type)

If the object referred by the variable on the left side of the operator passes th IS-A check for the class / interface type on the right side, then the result will be true. See the example.

Example 1

This will produce following result:
Output 1

This operator will still return true, if the object being compared is the assignment compatible with the type on the right. Now see the example 2.
Example 2


This will produce following result
Output 2

Precedence of Java Operators

Operator precedence determines the grouping of term in an expression. This affect how an expression is valuated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence that the addition operator. 

For example, X = 7 + 3 * 2; here X is assigned 13 not 20 because operator * has higher precedence that +, so it first gets multiplied with 3 * 2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
CategoryOperatorAssociativity
Postfix>() [] . (dot operator)Left toright
Unary>++ - - ! ~Right to left
Multiplicative>* /Left to right
Additive>+ -Left to right
Shift>>> >>> <<Left to right
Relational>> >= < <=Left to right
Equality>== !=Left to right
Bitwise AND>&Left to right
Bitwise XOR>^Left to right
Bitwise OR>|Left to right
Logical AND>&&Left to right
Logical OR>||Left to right
Conditional?:Right to left
Assignment>= += -= *= /= %= >>= <<= &= ^= |=Right to left
So, this is the end for my post about "Other" Basic Mathematical Operation. Hopefully this post can make you more and more understand about mathematical operators in Java. Wait for my next post. See Ya.


- FIN - 

[ DOWNLOAD SOURCE CODE ]
Java - Basic Operators. https://www.tutorialspoint.com/java/java_basic_operators.htm. Accessed at 25th and 26th of May 2017. 

Comments