相等,关系和条件运算符

平等和关系运算符

相等和关系运算符确定一个操作数是否大于,小于,等于或不等于另一操作数。这些操作员中的大多数也可能对您也很熟悉。请记住,在测试两个基本值是否相等时,必须使用“ ==”,而不是“ =”。

==      equal to
!=      not equal to
>       greater than
>=      greater than or equal to
<       less than
<=      less than or equal to

以下程序ComparisonDemo测试比较运算符:

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-AND Conditional-OR *操作。这些运算符表现出“短路”行为,这意味着仅在需要时才评估第二个操作数。

&& Conditional-AND
|| Conditional-OR

以下程序ConditionalDemo1测试了这些运算符:

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");
    }
}

另一个条件运算符是?:,可以将其视为if-then-else语句的简写(在本类的控制流语句部分中进行了讨论)。此运算符也称为三元运算符,因为它使用三个操作数。在下面的示例中,该运算符应读取为:“如果someConditiontrue,请将value1的值分配给result。否则,将value2的值分配给result。”

以下程序ConditionalDemo2测试?:运算符:

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);
    }
}

因为someCondition为 true,所以此程序在屏幕上打印“ 1”。如果它使您的代码更具可读性,请使用?:运算符而不是if-then-else语句;例如,当表达式紧凑且没有副作用(例如赋值)时。

类型比较运算符 instanceof

instanceof运算符将对象与指定类型进行比较。您可以使用它来测试对象是实现特定interface的类的实例,子类的实例还是类的实例。

以下程序InstanceofDemo定义了一个父类(名为Parent),一个简单interface(名为MyInterface)以及一个从父类继承并实现该interface的子类(名为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

使用instanceof运算符时,请记住null不是任何实例。