String对象实例化方式区别和intern()方法实现手工入池的使用

String 对象实例化两种方式的区别

  1.    直接赋值:只会产生一个实例化对象,并且库自动保存到对象池之中,以实现字符串实例的重用
  2. 构造方法:会产生两个实例化对象,并且不会自动入池,无法实现对象重用,但是可以利用intern()方法手工入池处理
  3. 池的概念:String对象(常量)池,该对象池的主要目的是实现数据的共享处理
  4. intern方法的使用:

例:

package day04;

public class StringIntern {
public static void main(String[] args) {
	System.out.print(" 未使用intern()方法手工入池前-->");
	String strA = "SCP";
	String strB = new String ("SCP");
	System.out.println(strA == strB);
	System.out.print(" 使用intern()方法手工入池后-->");

	String strC = "SCP";
	String strD = new String ("SCP").intern();
	System.out.println(strC == strD);
}
}
输出结果:  
             未使用intern()方法手工入池前-->false
             使用intern()方法手工入池后-->true

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值