JAVA String Detail

package com.java.string;

public class StringDetail {

  public static void main(String args[]) {

    String a = new String("hello");

    String b = "hello";

    String c = "hello";

    String d = "h" + "e" + "l" + "l" + "o";

    // false a 和 b 的地址不一样 a 会创建两个对象一个放在字符串池中, 一个放在堆中 a 和 b 地址指向都不一样,一个指向字符串池,一个指向堆
    System.out.println(a == b);

    // true a 和 b 的值 一样
    System.out.println(a.equals(b));

    // true b 和 c 指向同一个地址
    System.out.println(c == b);

    // true 由于常量的值在编译期就被确定了所以 String d = "h" +"e"+"l"+"l"+"o" 实际上就等于 String d = "hello";
    System.out.println(b == d);


    System.out.println("============================================================================");
    /**
    * JAVA编译器对string + 基本类型/常量 是当成常量表达式直接求值来优化的。
    * 运行期的两个string相加,会产生新的对象的,存储在堆(heap)中
    */
    String str6 = "b";
    String str7 = "a" + str6;
    String str67 = "ab";
    System.out.println("str7 = str67 : " + (str7 == str67));
    // ↑str6为变量,在运行期才会被解析。
    final String str8 = "b";
    String str9 = "a" + str8;
    String str89 = "ab";
    System.out.println("str9 = str89 : " + (str9 == str89));

  }
}

 

总结:

  1. 通过 String xx = "" 这种形式创建的字符串它是会存储在字符串池中的

  2. 通过 String xx = new String () 这种形式创建的字符串是会创建两个对象,一个放在字符串池中,一个放在堆中,地址指向堆(heap)中的对象

  3. 常量字符串相加在编译器就已经确定(优化) String xx = "a" + "b" + "c" = String xx = "abc";

  4. 字符串的变量相加就会产生新的字符串对象

  5. jdk1.5之后字符串 “+”连接内部是使用stringbuffer来实现但在很对字符串相加的情况下我们还得用stringbuffer来实现(参考:http://www.cnblogs.com/ITtangtang/p/3976820.html)

 

转载于:https://www.cnblogs.com/ivan920605/p/5849296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释这段代码package com.osm.domain; import com.fasterxml.jackson.annotation.JsonFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Good { private int id; private String goodName; private String detail; private String category; private double price; private int stock; private String img; public Good() { } public Good(int id, String name, String detail, String category, double price, int stock, String img) { this.id = id; this.goodName = name; this.detail = detail; this.category = category; this.price = price; this.stock = stock; this.img = img; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public String getImg() { return img; } public void setImg(String img) { this.img = img; } @Override public String toString() { return "Good{" + "id=" + id + ", goodName='" + goodName + '\'' + ", category='" + category + '\'' + ", detail='" + detail + '\'' + ", price=" + price + ", stock=" + stock + ", img='" + img + '\'' + '}'; } }
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值