JAVA中equals()方法的关键性
发布时间:2021-12-18 15:03:40 所属栏目:教程 来源:互联网
导读:对于对象比较使用equals()方法的重要性,这里以String类为例进行了比较。 /** * 对于对象比较使用equals()方法的重要性,这里以String类为例进行了比较。 * @author HAN * */ public class TestEqual { public TestEqual(){ testMethod(); } void testMethod(
对于对象比较使用equals()方法的重要性,这里以String类为例进行了比较。 /** * 对于对象比较使用equals()方法的重要性,这里以String类为例进行了比较。 * @author HAN * */ public class TestEqual { public TestEqual(){ testMethod(); } void testMethod(){ String str=new String("Gaowen HAN"); String str2=new String("Gaowen HAN"); String str3="Gaowen HAN"; String str4="Gaowen HAN"; if(str.equals(str2)){ System.out.println("str is equal to str2"); }else{ System.out.println("str is not equal to str2"); } if(str3==str4){ System.out.println("str is equal to str2"); }else{ System.out.println("str is not equal to str2"); } if(str==str2){ System.out.println("str is equal to str2"); }else{ System.out.println("str is not equal to str2"); } if(str.equals(str3)){ System.out.println("str is equal to str2"); }else{ System.out.println("str is not equal to str2"); } if(str==str3){ System.out.println("str is equal to str2"); }else{ System.out.println("str is not equal to str2"); } } public static void main(String[] args) { new TestEqual(); } } ![]() (编辑:我爱故事小小网_铜陵站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |