Java程序比较字符串是否相等
要在Java中比较字符串是否相等,请使用equals()方法。让我们看一些例子,其中我们检查了相同和不同的字符串值。
示例
public class Demo { public static void main(String[] args) { String one = "x"; String two = "x"; if(one.equals(two)) { System.out.println("String one is equal to two i.e. one==two"); }else{ System.out.println("String one is not equal to String two i.e. one!=two"); } }}输出结果
String one is equal to two i.e. one==two
让我们来看另一个例子。
示例
public class Demo { public static void main(String[] args) { String one = "x"; String two = "y"; if(one.equals(two)) { System.out.println("String one is equal to two i.e. one==two"); }else{ System.out.println("String one is not equal to String two i.e. one!=two"); } }}输出结果
String one is not equal to String two i.e. one!=two
热门推荐
10 祝女儿出嫁简短祝福语
11 恋爱祝福语大全简短搞笑
12 欢天喜地盖新房祝福语简短
13 干老师祝福语朋友简短
14 旅行前祝福语简短英语
15 宝宝祝福语诗意简短 古文
16 搬家祝福语卡片文案简短
17 女宝宝满月简短祝福语
18 别人转岗的祝福语简短