Equal and comparison Operators
It is equivalent to a comparison operator to determine whether an operand is greater than, less than, equal to, or not equal to another operand. The main operators are likely to be familiar to you. Remember, when determining whether two native types are equivalent, you must use "=" instead of "= ".
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
The followingProgram,Comparisondemo test comparison operator:
class ComparisonDemo {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if(value1 == value2)
System.out.println("value1 == value2");
if(value1 != value2)
System.out.println("value1 != value2");
if(value1 > value2)
System.out.println("value1 > value2");
if(value1 < value2)
System.out.println("value1 < value2");
if(value1 <= value2)
System.out.println("value1 <= value2");
}
}
Output:
value1 != value2
value1 < value2
value1 <= value2
Conditional Operators
& | The operator executes conditions and conditions or operations on two Boolean operands. These operators are short-circuited, which means that the second operand is calculated only when necessary.
& Conditions and | condition or
The following programConditionaldemo1, test the following OPERATOR:
class ConditionalDemo1 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if((value1 == 1) && (value2 == 2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1 == 1) || (value2 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
Another conditional operator is? : It can be considered as the stenographer of the if-then-else statement. It is the only ternary operator. In the following example, the operator can be considered as follows:Somecondition is true. value1 value is assigned to result. Otherwise, value2 value is assigned to result.
The following program,Conditionaldemo2, test? : Operator:
class ConditionalDemo2 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
System.out.println(result);
}
}
BecauseSomecondition is true, so the program outputs "1" in the screen. Use? : The operator replaces the if-then-else statement. If this causes yourCodeMore readable. For example, when the expression is compact and has no side effects (for example, assigning values ).
Type comparison operator instanceof
The instanceof operator compares an object with a specified type. It can be used to determine whether an object is an instance of a class, a subclass instance, and an instance of a class that implements a specific interface.
The following program,Instanceofdemo defines a parent class (named as parent) and a simple interface (the command isMyinterface), A subclass that inherits the parent class and implements the myinterface interface (named child)
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: "
+ (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: "
+ (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: "
+ (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: "
+ (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
Output:
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true
UseWhen using the instanceof operator, remember that null is not an instance of any class.