Java equals, comparison, and condition operators (translated from Java tutorials)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.