0
点赞
收藏
分享

微信扫一扫

字符串相关的类

字符串相关的类

String的特性

String类:代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作 为此类的实例实现。

  • String是一个final类,代表不可变的字符序列。
  • 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。
  • String对象的字符内容是存储在一个字符数组value[]中的。

源码

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0

String对象的创建

String str = "hello";

//本质上this.value = new char[0];

String s1 = new String();

//this.value = original.value;

String s2 = new String(String original);

//this.value = Arrays.copyOf(value, value.length);

String s3 = new String(char[] a);

String s4 = new String(char[] a,int startIndex,int count);

image-20230519210219209

image-20230519210230919

String str1 = “abc”;与String str2 = new String(“abc”);的区别?

字符串常量存储在 字符串常量池,目的是共享。字符串非常量对象 存储在堆中

image-20230519210320155

举报

相关推荐

0 条评论