【问题标题】:What is a Java String's default initial construction method? [duplicate]Java String 的默认初始构造方法是什么? [复制]
【发布时间】:2017-09-10 11:06:32
【问题描述】:
String str = "str";

当我像这样启动 String 对象时,它是如何工作的。它使用什么构造方法?

【问题讨论】:

  • this 帖子中的第一个答案有你想知道的一切

标签: java


【解决方案1】:

Oracle 文档中 String 类的详细信息:Refer this link

查看构造函数总结部分

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


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.


String(byte[] ascii, int hibyte)
Deprecated. 
This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a Charset, charset name, or that use the platform's default charset.


String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.


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


String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. 
This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a Charset, charset name, or that use the platform's default charset.


String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.


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


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


String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.


String(int[] codePoints, int offset, int count)
Allocates a new String that contains characters from a subarray of the Unicode code point array argument.


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(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

String(StringBuilder builder)

【讨论】:

    【解决方案2】:
    String str = "str";
    

    相当于:

    char strData[] = {'s', 't', 'r'};
    String str = new String( strData );
    

    它分配一个新的字符串,以便它表示当前包含在字符数组参数中的字符序列(在本例中为 strData[])。

    【讨论】:

    • 不,不是!! 1) 字符串必须被实习,2) 字符串的构造和实习必须在加载类时完成。这些使您的代码非常不同。
    • @StephenC 请看官方字符串docs
    • 文档过于简单化了。它在某些方面被证明是不等价的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2011-03-26
    • 2019-05-29
    • 2017-12-09
    • 1970-01-01
    相关资源
    最近更新 更多