What is the difference between new String() & new String(String str)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is the difference between new String() & new String(String str)?

2nd Mar 2017, 6:14 PM
Alok Agarwal
Alok Agarwal - avatar
15 Answers
+ 38
The difference is that new String() just allocates memory for a string but it will be empty. new String("coffee") allocates memory and saves the value "coffee" to it. Both have in common, that you shouldn't use them. To instantiate a string use String str = "coffee"; Because Java is able to reuse "coffee" internally then. If you create another string String str1 = "coffee"; Java notices that this string already exists in memory and will refer your str1 to the memory of str. That will result in only one object for both strings. Less memory loss -> better. Sly fox, Java ^.^
2nd Mar 2017, 6:29 PM
Tashi N
Tashi N - avatar
+ 8
@Mbonu Strings are character strings. Basically text.
14th Mar 2017, 5:57 PM
Tashi N
Tashi N - avatar
+ 3
new String(); creates string object with no value(also called as a NULL value). new String(String str); creates string object with value as that of passed string parameter
2nd Mar 2017, 6:19 PM
Meharban Singh
Meharban Singh - avatar
+ 2
new String() will create a String object and initialize it with null inside the default constructor. new String(String str) will create a String object and initialize it with null inside the parameterized constructor
7th Mar 2017, 4:40 AM
POJO
POJO - avatar
+ 2
String objects in Java are immutable, this means once the object is created, you can not change it. yes you can change the value of the variable with something else, but the variable will point to the new something else object. the original String object remains unchanged. to sum up: * New String() will create and point to an empty string object(ie: "") * New String(String str) is creating a new string object with the same value as str String object.
18th Mar 2017, 3:46 PM
Bingo
+ 1
String() is just a declared string with no contents while the other is initialized with str
7th Mar 2017, 2:10 PM
Dimitrios Haf
Dimitrios Haf - avatar
+ 1
String s1 = "Coskun";
14th Mar 2017, 1:13 PM
Programcı ABİ
Programcı ABİ - avatar
+ 1
new String() creates a null String object. new String(String str) which involves passing an actual parameter into the object creation creates a new String object and assigns its value to the variable str.
17th Mar 2017, 4:35 PM
Abdulhakeem
Abdulhakeem - avatar
0
new string occupies space for new string but when we call new string(string str) if occupies space as well as it put value in it string str
15th Mar 2017, 11:06 AM
Waqar Salim
Waqar Salim - avatar
0
didn't really get the concept at first. I also had an issue with x++ and +xx.
15th Mar 2017, 11:27 AM
Olawale
Olawale - avatar
0
It's an empty parameter while String(string str) has argument str in it.
16th Mar 2017, 5:58 PM
Shubham Sharma
Shubham Sharma - avatar
0
for the first case i.e String() you can allocate memory to a variable which will hold a String. for second case String(String str), you can actually pass any string as an argument and it is stored in the allocated memory
18th Mar 2017, 5:50 AM
Anubhav Malik
Anubhav Malik - avatar
0
it is this@@@@
18th Mar 2017, 11:47 AM
Ayato Camus Guei
Ayato Camus Guei - avatar
- 1
i think 'str' is just argument. Not different. You can use anything like 'a' or something. But You not must use it all.
14th Mar 2017, 5:53 PM
Nurafni Sagala
- 1
check jdkapi you know it
15th Mar 2017, 1:42 AM
xjxj