java中关系运算符_Java中的关系运算符

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个关系运算符。

  1. == is the equality operator. This returns true if both the operands are referring to the same object, otherwise false.

    ==是相等运算符。 如果两个操作数都引用同一个对象,则返回true,否则返回false。
  2. != is for non-equality operator. It returns true if both the operands are referring to the different objects, otherwise false.

    !=适用于非等号运算符。 如果两个操作数都引用不同的对象,则返回true,否则返回false。
  3. < is less than operator.

    <小于运算符。
  4. > is greater than operator.

    >大于运算符。
  5. <= is less than or equal to operator.

    <=小于或等于运算符。
  6. >= 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:

输出:

Relational Operators Java Example

Relational Operators Java Example

关系运算符Java示例

翻译自: https://www.journaldev.com/31883/relational-operators-in-java

java中关系运算符

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值