Java — Important Constructors of String Class | Code Factory

Code Factory
1 min readMay 14, 2020

--

Index Page : Link

Donate : Link

1. String s = new String();

- Create an empty String object in Heap

2. String s = new String(String literal);

- For given String literal equivalent object will be created in Heap area

- 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.

3. String s = new String(StringBuffer sb);

- Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.

4. String s = new String(StringBuilder sb);

- Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

5. String s = new String(char[] ch);

- Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.

6. String s = new String(byte[] b);

- Constructs a new String by decoding the specified array of bytes using the platform's default charset.

Reference : Link

--

--