day08-常用类_String的比较的方法_equals_equalsIgnoreCase

This commit is contained in:
2026-01-20 14:01:04 +08:00
parent d902faf12e
commit b8f24ac1a6

View File

@@ -0,0 +1,30 @@
package com.inmind.string01;
/*
常用类_String的比较的方法_equals_equalsIgnoreCase
String的比较
==表示比较String对象的地址其实没有任何意义==一般用于基本数据类型的值是否相等比较
equals表示比较String对象地址所指向的内容
比较的方法:
public boolean equals (Object anObject) :将此字符串与指定对象进行比较。
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小
写。
*/
public class Demo03 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = new String("abc");
System.out.println(str1);
System.out.println(str2);
//比较字符串的内容是否相同
System.out.println(str1 == str2);//比较地址,错误操作
boolean result = str1.equals(str2);//比较内容,正确操作
System.out.println(result);//true
String str3 = "AbC";
System.out.println(str1.equals(str3));//false
System.out.println(str1.equalsIgnoreCase(str3));//true
}
}