Java里的==和equals到底怎么用

代码中最常写的int和String的判断,我的理解是数字就用==,字符串就用equals来判断,直到同事给我改了一个类似
在这里插入图片描述
在这里插入图片描述
我才反应,数字也可以用equals判断啊。
估计同事无语了,让我去查一下 == 和equals的区别,翻了好几个文章,都在说“地址”“栈”“内存”,看的脑袋疼。
又找了几个才终于反应过来,下面是测试结果。
基本数据类型有:char、boolean、byte、short、int、long、float、double

//char、boolean、byte、short、int、long、float、double
		char ch1 = 'a';
		char ch2 = 'a';
		if(ch1 == ch2) {
			System.out.println("char类型可以用==判断");
		}
		boolean bo1 = true;
		boolean bo2 = true;
		if(bo1 == bo2) {
			System.out.println("boolean类型可以用==判断");
		}
		byte by1 = 1;
		byte by2 = 1;
		if(by1 == by2) {
			System.out.println("byte类型可以用==判断");
		}
		short sh1 = 1;
		short sh2 = 1;
		if(sh1 == sh2) {
			System.out.println("short类型可以用==判断");
		}
		int i1 = 1;
		int i2 = 1;
		if(i1 == i2) {
			System.out.println("int类型可以用==判断");
		}
		long l1 = 1L;
		long l2 = 1L;
		if(l1 == l2) {
			System.out.println("long类型可以用==判断");
		}
		float f1 = 1.1f;
		float f2 = 1.1f;
		if(f1 == f2) {
			System.out.println("float类型可以用==判断");
		}
		double d1 = 1.1d;
		double d2 = 1.1d;
		if(d1 == d2) {
			System.out.println("double类型可以用==判断");
		}

在这里插入图片描述
基本数据类型的包装类

		Character ch1 = 'a';
		Character ch2 = 'a';
		if(ch1 == ch2) {
			System.out.println("Character类型可以用==判断");
		}
		if(ch1.equals(ch2)) {
			System.out.println("Character类型可以用equals判断");
		}
		Boolean bo1 = true;
		Boolean bo2 = true;
		if(bo1 == bo2) {
			System.out.println("Boolean类型可以用==判断");
		}
		if(bo1.equals(bo2)) {
			System.out.println("Boolean类型可以用equals判断");
		}
		Byte by1 = 1;
		Byte by2 = 1;
		if(by1 == by2) {
			System.out.println("Byte类型可以用==判断");
		}
		if(by1.equals(by2)) {
			System.out.println("Byte类型可以用equals判断");
		}
		Short sh1 = 1;
		Short sh2 = 1;
		if(sh1 == sh2) {
			System.out.println("Short类型可以用==判断");
		}
		if(sh1.equals(sh2)) {
			System.out.println("Short类型可以用equals判断");
		}
		Integer i1 = 1;
		Integer i2 = 1;
		if(i1 == i2) {
			System.out.println("Integer类型可以用==判断");
		}
		if(i1.equals(i2)) {
			System.out.println("Integer类型可以用equals判断");
		}
		Long l1 = 1L;
		Long l2 = 1L;
		if(l1 == l2) {
			System.out.println("Long类型可以用==判断");
		}
		if(l1.equals(l2)) {
			System.out.println("Long类型可以用equals判断");
		}
		Float f1 = 1.1f;
		Float f2 = 1.1f;
		if(f1 == f2) {
			System.out.println("Float类型可以用==判断");
		}
		if(f1.equals(f1)) {
			System.out.println("Float类型可以用equals判断");
		}
		Double d1 = 1.1d;
		Double d2 = 1.1d;
		if(d1 == d2) {
			System.out.println("Double类型可以用==判断");
		}
		if(d1.equals(d2)) {
			System.out.println("Double类型可以用equals判断");
		}

在这里插入图片描述
基本数据类型的包装类中Float和Double只能用equals不能用==
引用类型String
下面展示一些 内联代码片

String str1 = "abc";
		String str2 = "abc";
		String str3 = new String("abc");
		String str4 = new String("abc");
		if(str1 == str2) {
			System.out.println("String类型可以用==判断");
		}
		if(str1.equals(str2)) {
			System.out.println("String类型可以用equals判断");
		}
		if(str3 == str4) {
			System.out.println("String类型可以用==判断");
		}
		if(str3.equals(str4)) {
			System.out.println("String类型可以用equals判断");
		}

在这里插入图片描述
String类型,直接赋值的可以用 == 也可以用equals,但是new出来的只能用equals。
为了方便记忆,基本数据类型用 == 判断,包装类和引用类型String都用equals判断。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值