比较两个对象

来源:互联网 发布:代理模式java有几种 编辑:程序博客网 时间:2024/06/05 07:06

package com.javaio.test;

public class TestString1 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String a = "dong";
  String b = "dong";

  String c = new String("dong");
  String d = new String("dong");

  if (a == b) {
   System.out.println("地址对象  " + "true");
  }

  if (a.hashCode() == b.hashCode()) {
   // System.out.println(a.hashCode());
   // System.out.println(b.hashCode());
   System.out.println("hashCode是否相等  " + "true");
  }

  if (a.endsWith(b)) {
   System.out.println("值是否相同  " + "true");
  }
  System.out.println("=======================================");
  if (a == c) {
   System.out.println("地址对象  " + "true");
  }

  if (a.hashCode() == c.hashCode()) {
   // System.out.println(a.hashCode());
   // System.out.println(b.hashCode());
   System.out.println("hashCode是否相等  " + "true");
  }

  if (a.endsWith(c)) {
   System.out.println("值是否相同  " + "true");
  }
  System.out.println("-------------------------------");
  if (d == c) {
   System.out.println("地址对象  " + "true");
  }

  if (d.hashCode() == c.hashCode()) {
   // System.out.println(a.hashCode());
   // System.out.println(b.hashCode());
   System.out.println("hashCode是否相等  " + "true");
  }

  if (d.endsWith(c)) {
   System.out.println("值是否相同  " + "true");
  }
 }

}

 

--------------------------------

地址对象  true
hashCode是否相等  true
值是否相同  true
=======================================
hashCode是否相等  true
值是否相同  true
-------------------------------
hashCode是否相等  true
值是否相同  true

原创粉丝点击