+ 4
What is the difference between new String() & new String(String str)?
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 ^.^
+ 8
@Mbonu Strings are character strings. Basically text.
+ 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
+ 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
+ 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.
+ 1
String() is just a declared string with no contents while the other is initialized with str
+ 1
String s1 = "Coskun";
+ 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.
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
0
didn't really get the concept at first.
I also had an issue with x++ and +xx.
0
It's an empty parameter while String(string str) has argument str in it.
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
0
it is this@@@@
- 1
i think 'str' is just argument. Not different. You can use anything like 'a' or something. But You not must use it all.
- 1
check jdkapi
you know it