java中关系运算符
Relational Operators in Java are used to comparing two variables for equality, non-equality, greater than, less than, etc. Java relational operator always returns a boolean value – true or false.
Java中的关系运算符用于比较两个变量是否相等,不相等,大于,小于等。Java关系运算符始终返回布尔值-true或false。
Java中的关系运算符 (Relational Operators in Java)
Java has 6 relational operators.
Java有6个关系运算符。
- == is the equality operator. This returns true if both the operands are referring to the same object, otherwise false. ==是相等运算符。 如果两个操作数都引用同一个对象,则返回true,否则返回false。
- != is for non-equality operator. It returns true if both the operands are referring to the different objects, otherwise false. !=适用于非等号运算符。 如果两个操作数都引用不同的对象,则返回true,否则返回false。
- < is less than operator. <小于运算符。
- > is greater than operator. >大于运算符。
- <= is less than or equal to operator. <=小于或等于运算符。
- >= is greater than or equal to operator. > =大于或等于运算符。
关系运算符支持的数据类型 (Relational Operators Supported Data Types)
- The == and != operators can be used with any primitive data types as well as objects. ==和!=运算符可与任何原始数据类型以及对象一起使用。
- The <, >, <=, and >= can be used with primitive data types that can be represented in numbers. It will work with char, byte, short, int, etc. but not with boolean. These operators are not supported for objects. <,>,<=和> =可以与可以用数字表示的原始数据类型一起使用。 它适用于char,byte,short,int等,但不适用于boolean。 对象不支持这些运算符。
关系运算符示例 (Relational Operators Example)
package com.journaldev.java;
public class RelationalOperators {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
// objects support == and != operators
System.out.println(new Data1() == new Data1());
System.out.println(new Data1() != new Data1());
}
}
class Data1 {
}
Output:
输出:
翻译自: https://www.journaldev.com/31883/relational-operators-in-java
java中关系运算符