package com.test7; public class test7 { public static void main(String[] args) { /** * 1、equals()比较的是对象的内容(区分字母的大小写格式)是否相等 equals()用于判断值内容 * 2、“==”比较两个对象时,比较的是两个对象的内存地址, */ int a = 1; int b = 1; String s1 = "abc"; String s2 = "abc"; String s3 = new String("abc"); //new创建对象 和上面直接赋值内存地址不一样 String s4 = new String("abc"); System.out.println("s1与s2 ==相等" + (s1 == s2)); System.out.println("s1与s2 equals相等" + s1.equals(s2)); System.out.println("s1与s3 ==相等" + (s1 == s3)); System.out.println("s1与s3 equals相等" + s1.equals(s3)); System.out.println("s3与s4 ==相等" + (s3 == s4)); System.out.println("s3与s4 equals相等" + s3.equals(s4)); System.out.println("a与b ==相等" + (a == b)); //System.out.println("a与b equals相等" + a.equals(b)); 会报错 /** 总结 * equals比较内容值的话,四个都相等 * 打印结果如下 s1与s2 ==相等true s1与s2 equals相等true s1与s3 ==相等false s1与s3 equals相等true s3与s4 ==相等false s3与s4 equals相等true a与b ==相等true */ } }