Sting

String 有关基础问题

 

String:实际上是字符串常量
常量的理解就非常的明确
  1. /** The value is used for character storage. */  
  2. private final char value[];  
  3.   
  4. /** The offset is the first index of the storage that is used. */  
  5. private final int offset;  
  6.   
  7. /** The count is the number of characters in the String. */  
  8. private final int count; 
用于存放字符的数组被声明为final的,因此只能赋值一次,不可再更改。

 

而String 实例化过程如下:
String a = "123";
用以上这种方法进行String创建时,实际java首先在内存中寻找"123"字符串,如果有,就把"123"的
地址给它,若没有则创建,
但是以下面这种方式创建
String a = new String("123");
是不管内存中有没有"123"
都开辟一块新内存保存它
  1. /** The value is used for character storage. */  
  2. public static void main(String[] args) {
  3. String a = "123";
  4. System.out.println(a.hashCode());
  5. String c = "123";
  6. System.out.println(c.hashCode());
  7. }
输出结果为
48690
48690
他们结果是一样,他们都是由相同的指针指向
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
02-23 889
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值