Java 中 String 的构造方法

String 对于所有 Java 程序员来说都不会陌生,几乎每天甚至每个程序都会和 String 打交道,因此将 String 的常用知识汇集在此,方便查阅。

概叙:

Java 中是如此定义 String 的:

public final class String extends Object implements Serializable, Comparable<String>, CharSequence

String 是 final 类型的,继续看下面的解释:

The String class represents character strings. 

All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. 

String 对象代表了字符的串或序列,Java 中所有字面量的字符串都是 String 的对象;

String 是常量,它的值一旦被创建就不能改变了。

String buffers support mutable strings. Because String objects are immutable they can be shared. 

接下来说字符缓冲区支持可变的字符串,因为缓冲区里面的字符串对象们可以被共享(其实就是使对象的引用发生了改变)。

 

在 Java 中,提供了通过字符串连接符( + )号,将其他类型的对象转换成 String 类型:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.

int i = 123;
String str2 = i + "abc"; /** * 上面一句生成的 class 文件如下: * String str2 = (new StringBuilder(String.valueOf(i))).append("abc").toString(); * String 对 “+” 的支持使用了 StringBuilder 以及它的 append 和 toString 方法。 */

表面看似简单,其实运算复杂,不如直接用 String.valueOf 来的快:

int i = 1;
String strNew = String.valueOf(1) + "abc";

构造方法:

1)无参构造方法

String()
Initializes a newly created String object so that it represents an empty character sequence.

该构造方法会创建空的字符序列,注意这个构造方法的使用,因为创造不必要的字符串对象是不可变的。因此不建议采取下面的创建 String 对象:

String str = new String()
str = "sample";

这样的结果可想而知,产生了不必要的对象。

2)参数为字符串类型

String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

源 String 的值不会影响目标 String 的值,但是用到比较少,或许结合 subString 方法使用:

String original = "1234567890";
String str = new String(original.subString(2, 5));

这样写是有必要的,当 original 是个大字符串时,这种截取方法可以使得新的字符串对象指向新的对象,而不是原来的 original,减少内存占用。

3)参数为 byte 数组类型

String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform's default charset.

String(byte[] bytes, Charset charset)
Constructs a new String by decoding the specified array of bytes using the specified charset.

byte 是网络传输或存储的序列化形式,所以在很多传输和存储的过程中需要将 byte[] 数组和 String 进行相互转化。

有关 IO 流的代码中会出现这个构造方法,字符编码导致的乱码问题也是比较多的,这里需要注意:一旦看到 byte 数组,一定要想到编码问题

经常会用到 getBytes(Charset charset) 方法:

try {
  byte[] byteArr = strNew.getBytes("UTF-8");
  String newString = new String(byteArr, "UTF-8");
} catch (UnsupportedEncodingException e) {
  e.printStackTrace();
}

4)参数为 char 数组类型

String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

这里值得注意的是:当我们使用字符数组创建 String 的时候,会用到 Arrays.copyOf 方法或 Arrays.copyOfRange 方法。这两个方法是将原有的字符数组中的内容逐一的复制到 String 中的字符数组中。会创建一个新的字符串对象,随后修改的字符数组不影响新创建的字符串。

public String(char value[]) {
  this.value = Arrays.copyOf(value, value.length);
}

5)参数为 StringBuilder 或者 StringBuffer 类型

String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

个人认为这种方式的构造方法用的比较少吧,如果是这两种类型的,用 toString 方法的更方便一些。

StringBuilder sbb = new StringBuilder();
sbb.append("hello ").append("world");
String str1 = sbb.toString();

特别感谢以下博文:

Java 源码分析 —— String 的设计

Java 中 String 与 byte[] 的转换

转载于:https://www.cnblogs.com/JavaSubin/p/5450849.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值