目录
public String(byte[] bytes,int offset,int length)
public String(char[] value,int offset,int count)
public String(String original)
点击下方链接学习更多的String类 API深入研究
Sring类的判断功能 API深入研究2.0
String类的获取功能 API深入研究3.0
String类的字符串转换功能 API深入研究4.0
String类的替换功能、去除字符串两端空格、按字典顺序比较两个字符串 API深入研究5.0
在我们企业的日常开发中,public String()是每天都会去使用的高频构造方法,那么我们来深入探索一下。(个人参考API文档编写,仅供学习参考)
字符串:简单理解,就是由多个字符组成的数据,叫做字符串;也可以看作是一个字符数组。
观察API发现:
1、String代表的是字符串,属于java.lang下面的。所以使用的时候不需要导包。
2、String类代表字符串,Java程序中的所有字符串文字(例如"abc"),都被实现为此类的实例(对象)。
3、字符串不变:它们的值在创建后不能被更改,字符串是常量,一旦被赋值,就不能改变。
注意事项:String类重写了toString()方法。
构造方法详细信息:
public String()
查看API文档我们知道:
初始化新创建的String
对象,以使其表示空字符序列。 请注意,使用此构造函数是不必要的,因为Strings是不可变的。
打开String的原码可以发现String是被final默认修饰的,代表的是常量是不可改变的值。
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
//public String()
String s = new String();
System.out.println("s: " + s);
}
}
s:
Process finished with exit code 0
案例:字符串是常量,它的值在创建之后不能更改
String s = “hello”;
s += “world”;
问s的结果是多少?
参考代码:
public class StringDemo2 {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println(s);
}
}
helloworld
Process finished with exit code 0
我们打开原码可以看到:
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
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written into an ObjectOutputStream according to
* <a href="{@docRoot}/../platform/serialization/spec/output.html">
* Object Serialization Specification, Section 6.2, "Stream Elements"</a>
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
}
String是被final默认修饰的,所以不能修改。
参考图示:
public String(byte[] bytes)
查看API文档我们知道:
public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
构造一个新的String
由指定用指定的字节的数组解码charset 。 新的String
的长度是字符集的函数,因此可能不等于字节数组的长度。给定字节在给定字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
参数:bytes
- 要解码为字符的字节
charsetName
- 支持的名称charset
异常:UnsupportedEncodingException
- 如果不支持命名的字符集
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
//public String(byte[] bytes) 将一个字节数组转成一个字符串
byte[] b = {97,98,99,100,101};
String s2 = new String(b);
System.out.println("s2: " + s2);
}
}
s2: abcde
Process finished with exit code 0
public String(byte[] bytes,int offset,int length)
查看API文档我们知道:
public int length()
返回此字符串的长度。 长度等于字符串中的数字Unicode code units 。
Specified by:
结果
由该对象表示的字符序列的长度。
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
//public String()
String s = new String();
System.out.println("s: " + s);
String s1 = new String("qwerdf");
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s.length());
System.out.println("字符串s的长度为:" + s1.length());
}
}
s:
字符串s的长度为:0
字符串s的长度为:6Process finished with exit code 0
public String(byte[] bytes, int offset, int length)
查看API文档我们知道:
通过使用平台的默认字符集解码指定的字节子阵列来构造新的String
。 新的String
的长度是字符集的函数,因此可能不等于子数组的长度。
指定字节在默认字符集中无效时,此构造函数的行为是未指定的。 当需要更多的解码过程控制时,应使用CharsetDecoder类。
参数
bytes
- 要解码为字符的字节
offset
- 要解码的第一个字节的索引
length
- 要解码的字节数
异常
IndexOutOfBoundsException
- 如果 offset
和 length
参数的索引字符在 bytes
数组的边界之外
参考代码2:
public class testStringDemo1 {
public static void main(String[] args) {
byte[] b = {97,98,99,100,101};
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分截取出来变成一个字符串
String s3 = new String(b, 1, 3);
System.out.println("s3: " + s3);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s3.length());
}
}
s3: bcd
字符串s的长度为:3Process finished with exit code 0
参考代码3:
public class testStringDemo1 {
public static void main(String[] args) {
byte[] b = {97,98,99,100,101};
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分截取出来变成一个字符串
String s3 = new String(b, 1, 3);
System.out.println("s3: " + s3);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s3.length());
System.out.println("***************************************");
//StringIndexOutOfBoundsException
String s4 = new String(b, 1, 5);
System.out.println("s4: " + s4);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s4.length());
}
}
我们观察s4的运行结果:
如果截取的lenth长度大于字节数组的长度则会出现报错:
StringIndexOutOfBoundsException
字符串截取下标越界
public String(char[] value)
查看API文档我们知道:
分配一个新的String
,以便它表示当前包含在字符数组参数中的字符序列。 字符数组的内容被复制; 字符数组的后续修改不会影响新创建的字符串。
参数
value
- 字符串的初始值
顾名思义就是将一个字符数组转换成一个字符串。
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
//public String(char[] value) 将一个字符数组转成一个字符串
char[] c = {'a','b','c','d','我','爱','写','代','码'};
String s5 = new String(c);
System.out.println("s5: " + s5);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s5.length());
}
}
s5: abcd我爱写代码
字符串s的长度为:9Process finished with exit code 0
public String(char[] value,int offset,int count)
查看API文档我们知道:
分配一个新的String
,其中包含字符数组参数的子阵列中的字符。 offset
参数是子阵列的第一个字符的索引, count
参数指定子阵列的长度。 副本的内容被复制; 字符数组的后续修改不会影响新创建的字符串。
参数
value
- 作为字符源的数组
offset
- 初始偏移量
count
- 长度
异常
IndexOutOfBoundsException
- 如果 offset
和 count
参数的索引字符超出了 value
数组的界限
顾名思义就是将字符数组中的一部分截取出来,变成一个字符串。
参考代码1:
public class testStringDemo1 {
public static void main(String[] args) {
//public String(char[] value) 将一个字符数组转成一个字符串
char[] c = {'a','b','c','d','我','爱','写','代','码'};
String s5 = new String(c);
System.out.println("s5: " + s5);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s5.length());
System.out.println("***************************************");
//public String(char[] value,int offset,int count)
//将字符数组中一部分截取出来变成一个字符串
String s6 = new String(c, 4, 5);
System.out.println("s6: " + s6);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s6.length());
}
}
s5: abcd我爱写代码
字符串s的长度为:9
***************************************
s6: 我爱写代码
字符串s的长度为:5Process finished with exit code 0
观察API文档我们得知如果count的长度字符数组的长度则会出现异常。
参考代码2:
public class testStringDemo1 {
public static void main(String[] args) {
//public String(char[] value) 将一个字符数组转成一个字符串
char[] c = {'a','b','c','d','我','爱','写','代','码'};
String s5 = new String(c);
System.out.println("s5: " + s5);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s5.length());
System.out.println("***************************************");
//public String(char[] value,int offset,int count)
//将字符数组中一部分截取出来变成一个字符串
String s6 = new String(c, 4, 5);
System.out.println("s6: " + s6);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s6.length());
System.out.println("***************************************");
String s7 = new String(c,4,6);
System.out.println("s7:" + s7);
}
}
我们观察S7的运行结果:
当字符串count截取长度大于字符数组的长度则会报错:
StringIndexOutOfBoundsException
字符串截取下标越界
public String(String original)
查看API文档我们知道:
初始化新创建的String
对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。 除非需要original
的显式副本, original
使用此构造函数是不必要的,因为Strings是不可变的。
参数
original
- A String
参考代码:
public static void main(String[] args) {
//public String(String original)
String s7 = "你好";
String s8 = new String(s7);
System.out.println("s8: " + s8);
//查看字符串的长度
//public int length()返回此字符串的长度。
System.out.println("字符串s的长度为:" + s8.length());
}
}
s8: 你好
字符串s的长度为:2Process finished with exit code 0
参考图示:
到底啦!你有学到什么吗?来给靓仔一个关注吧!