java常用类库String

代码示例:

1.直接赋值和创建对象区别
String s1="小白";
String s2=new String("小白");

System.out.println(s1==s2);
/*
结果:false
s1在常量池中,s2是在堆上创建的对象,二者内存地址不相等
*/
2.如果在编译期值可以被确定,那么使用已有对象,不能则创建对象
String s3="a";
String s4=s3+1;//s3值不能被确定,相当于new了一个对象
String s5="a1";//s5值能被确定,使用常量池中的"a1"

System.out.println(s4==s5);
/*
结果:false
*/

final String s6="a";//final定义一个常量
String s7=s6+2;//s6值能被确定
String s8="a2";

System.out.println(s7==s8);
/*
结果:true
*/

3.方法只有在运行的时候才会执行

public static void getB(){
	return "b";
}

public static void getB(){
	return "c";
}

String  b1=getB;
String b2=b1+1;
String b3="b1";
System.out.println(b2==b3);
/*
结果:false
*/

final String  c1=getC;
String c2=c1+1;
String c3="b1";
System.out.println(c2==c3);
/*
结果:false
*/

源码剖析

@Stable
private final byte[] value;//第一次赋值后不能修改
private final byte coder;//第一次赋值后不能修改
private int hash; // Default to 0

/**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
*
* @param  value
*         The initial value of the string
*/
@HotSpotIntrinsicCandidate
public String(String original) {
    this.value = original.value;
    this.coder = original.coder;
    this.hash = original.hash;
 }

中文文档:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值