-4
String str = "str";

When I initiate String object like this, how it works. What construction method does it use?

Sean Zhang
  • 108
  • 6
  • 1
    First answer in [this](http://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-col) post has all you want to know – Robin Topper Apr 14 '17 at 07:41

2 Answers2

0

Details of String class from Oracle docs : Refer this link

See the constructor summary section

    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)
-1
String str = "str";

is equivalent to:

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

it allocates a new String so that it represents the sequence of characters currently contained in the character array argument( strData[ ] in this case ).

ShahzadIftikhar
  • 515
  • 2
  • 11
  • 1
    No it isn't!! 1) the string must be interned, and 2) the construction and interning of the string must be done *when the class is loaded*. These make your code very different. – Stephen C Apr 14 '17 at 10:46
  • @StephenC please have a look at official String [docs](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html). – ShahzadIftikhar Apr 14 '17 at 11:39
  • The docs are oversimplifying things. It is provabling not equivalent in certain respects. – Stephen C Apr 14 '17 at 13:51