java判断char是否相等

在 Java 中可以使用 "==" 运算符来判断两个 char 类型的变量是否相等。例如:

char c1 = 'a'; char c2 = 'b';

if (c1 == c2) { System.out.println("c1 is equal to c2"); } else { System.out.println("c1 is not equal to c2"); }

输出结果为 "c1 is not equal to c2"。

需要注意的是,如果你想比较两个字符串是否相等,那么应该使用 equals() 方法,而不是 "==" 运算符。例如:

String s1 = "hello"; String s2 = "world";

if (s1.equals(s2)) { System.out.println("s1 is equal to s2"); } else { System.out.println("s1 is not equal to s2"); }

输出结果为 "s1 is not equal to s2"。

如果你想比较两个对象的引用是否指向同一个对象,那么可以使用 "==" 运算符。例如:

String s1 = new String("hello"); String s2 = s1;

if (s1 == s2) { System.out.println("s1 and s2 refer to the same object"); } else { System.out.println("s1 and s2 do not refer to the same object"); }

输出结果为 "s1 and s2 refer to the same object"。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值