String 初始化 equals ==

</pre><ol><li><span style="font-family:Arial; font-size:14px; line-height:25px">一、 </span><span style="font-family:Arial; font-size:14px; line-height:25px">String初始化:</span></li></ol><p></p><p><span style="font-family:Arial; font-size:14px; line-height:25px"><span style="white-space:pre"></span>有两种方法,都是返回一个String对象的引用,但是jvm对两者的处理方式是不一样的。</span></p><p></p><ol><li><span style="font-family:Arial; font-size:14px; line-height:25px">第一种,jvm会马上在heap(堆)中创建一个String对象,然后将该对象的引用返回给用户。</span></li><li><span style="font-family:Arial; font-size:14px; line-height:25px">第二种,jvm首先会在内部维护的strings pool中通过String的 equels 方法查找是对象池中是否存放有该String对象:</span></li></ol><span style="font-family:Arial; font-size:14px; line-height:25px"></span><ul><li>如果有,则返回已有的String对象给用户,而不会在heap中重新创建一个新的String对象;</li><li>如果没有,jvm则在heap中创建新的String对象,将其引用返回给用户,同时将该引用添加至strings pool中。</li></ul><div> 二、比较 == 和 equals</div><div><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; color:rgb(57,57,57); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:19.0909px; background-color:rgb(250,247,239)"></p><ol><li><span style="line-height:19.0909px; margin:0px; padding:0px">"=="操作符的作用</span></li></ol><p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; color:rgb(57,57,57); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:19.0909px; background-color:rgb(250,247,239)"></p><ul><li><span style="line-height:19.0909px">用于基本数据类型的比较;</span></li><li><span style="line-height:19.0909px">判断引用是否指向堆内存的同一块地址。</span></li></ul><div>   2.equals</div><div><ul><li><span style="line-height:1.5">equals所在位置:</span></li></ul></div><p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; color:rgb(57,57,57); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:19.0909px; background-color:rgb(250,247,239)"><span style="margin:0px; padding:0px; line-height:1.5"><span style="white-space:pre"></span>在Object类当中,而Object是所有类的父类,包含在jdk里面,但并不适合绝大多数场景,通常需要重写:</span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; color:rgb(57,57,57); font-family:Verdana,Arial,Helvetica,sans-serif; font-size:14px; line-height:19.0909px; background-color:rgb(250,247,239)"><span style="margin:0px; padding:0px; line-height:1.5"></span></p><pre name="code" class="java">public boolean equals(Object obj) {
        return (this == obj);
    }

  • equals的作用:

用于判断两个变量是否是对同一个对象的引用,即堆中的内容是否相同,返回值为布尔类型

boolean b = obj1.equals(obj2);
例子:
<pre name="code" class="java">public class Initialize {

	public static void main(String[] args) {
		String str1 = new String("abc");
//	str2和str3是内存的同一内容,不同名;
		String str2 = "abc";
		String str3 = "abc";
		
//   	== 判断引用是否指向堆内存的同一块地址	
		if (str1 == str2){
			System.out.println("str1 == str2");
		}
		if (str1 != str2) {
			System.out.println("str1 != str2");
		}

		if (str2 == str3){
			System.out.println("str1 2== str3");
		}

		
		System.out.println("str1.equals(str2) = " + str1.equals(str2) );
		System.out.println("str2.equals(str3) = " + str2.equals(str3) );
	}

}


 输出为: 

str1 != str2
str1 2== str3
str1.equals(str2) = true
str2.equals(str3) = true






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值