欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA中equals()方法的重要性

发布时间:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JAVA中equals()方法的重要性 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

对于对象比较使用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();} }

转载于:https://www.cnblogs.com/javaTest/archive/2011/12/29/2589215.html

总结

以上是生活随笔为你收集整理的JAVA中equals()方法的重要性的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。