Java Basics: Comparison Operators

The operation operators introduced in the previous article, this article introduces the comparison operators.

I. Overview

There are generally the following comparison operators in Java:

operator example explain
> x > y Returns true if x is greater than y.
>= x >= y Returns true if x is greater than or equal to y.
< x < y Returns true if x is less than y.
<= x <= y Returns true if x is less than or equal to y.
== x == y Returns true if x is equal to y.
!= x != y Returns true if x and y are not equal.
example x instance of y Returns true if x is of the same class as y or a subclass of y.

Code example:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/1 16:44
 * @description:
 */
public class Four {

    public static void main(String[] args) {
        
        int a = 100;
        double b = 100.001D;

        if (b > a) {
            System.out.println("b > a 为" + (b > a));
        }

        if (b >= a) {
            System.out.println("b >= a 为" + (b >= a));
        }

        if (a < b) {
            System.out.println("a < b 为" + (a < b));
        }

        if (a <= b) {
            System.out.println("a <= b 为" + (a <= b));
        }
    }
}

Results of the:

As for the size comparison, I believe everyone knows the result, so I won’t explain it here.

2. Use == and !=

useand != Note: Java data types are roughly divided into two types: primitive types and reference types . used in primitive typesand != are ok, but be careful when using them on reference types.

==!= Pay attention when using it on a reference type: you are not comparing the value held by the object, but whether the referrer of the object reference is the same. If you want to compare the value held by the object, you can use the equals method.

Same value, same reference:

The values ​​are the same, but the references are different:

Let's go directly to the code:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/1 17:14
 * @description:
 */
public class Five {

    public static void main(String[] args) {
        StringBuffer x = new StringBuffer("xyz");
        StringBuffer y = x;
        System.out.println("x和y比较为" + (x == y));

        StringBuffer z = new StringBuffer("xyz");
        StringBuffer w = new StringBuffer("xyz");
        System.out.println("z和w比较为" + (z == w));
    }
}

Results of the:

Code explanation:

  • Create an object x using the new operator.
  • Using the assignment operator = copies object x to object y, in this case just copying the reference information of x into y instead of reallocating the area of ​​y in the memory area.
  • Comparing x and y with the == operator returns true because the reference addresses are the same.
  • Object z is created using the new operator.
  • Object w is created using the new operator.
  • Comparing z and w with the == operator returns false because the value abc is the same but has a different reference address.

3. Use of instanceof

instanceof is an operator that tests whether the left operand is of the same class or subclass as the right operand. The left operand can be any object reference expression (variable, etc.), and the right operand can be a class, interface, or array type .

Let's demonstrate it through code:

father:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/1 17:38
 * @description:
 */
public class Six {

    public static void main(String[] args) {
        SixSub sub = new SixSub();

        if (sub instanceof Six) {
            System.out.println("sub属于Six类");
        } else {
            System.out.println("sub不属于Six类");
        }
    }
}

Subclass:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/1 17:38
 * @description:
 */
public class SixSub extends Six {
}

Execute the main method of the parent class:

Code explanation:

  • Declare and generate an object variable sub whose value is of class SixSub.
  • Check whether the object variable sub is the same class or a subclass of the Six class using the operator instanceof. Returns true because sub has data type SixSub class is a subclass of Six class.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132215457