In this section you will learn about the Other Operators. This is one type of operators.
Other Operators in java 7
In this section you will learn about the Other Operators. This is one type of operators.
Conditional Operator (? :):
This is also called ternary operator. It contains three operand and two operators. ? and : combined used for ternary operator. It works like if-else statement so you can say ternary operator is short form of if-else statement. So it checks the condition and if it is true then process first expression otherwise process another expression.
Syntax :
boolean Expression ? expression1 : expression2;
Example :
In this example we are using ternary operator to print the highest number in x and y.
package operator; class ConditionalOperator { public static void main(String[] args) { int x = 10; int y = 20; int z; z = (x > y ? x : y); //Conditional Operator System.out.println("Highest number in x and y : " + z); } }
Output :
Highest number in x and y : 20
Instance operators :
It is another kind of operator used to compare the object to the given
type. It is used to test if an object is an instance of a class, an instance of
a subclass, or an instance of a class that implements a particular interface
package operator; class InstanceOfOperator { public static void main(String[] args) { ArithmaticOperator operator = new ArithmaticOperator(); boolean result = operator instanceof ArithmaticOperator; System.out.println("Instance Operator : " + result); } }
Output :
Instance Operator : true