Describe the effect of the following Java statements String
Describe the effect of the following Java statements String
str1, str2, str3, str4; //line 1
str1 = \"Java Programming\"; //line 2
str2 = \"Java Programming\"; //line 3
str3 = new String(\"Java Programming\"); //line 4
str4 = new String(\"Java Programming\"); //line 5
Solution
There are two ways of defining string in Java one is literal and other is with new keyword.And when we do it using literal way then t doesn\'t create a sepearte memory space just the string varaible points to one string in string pool.
Line1 declares four string variables names str1,str2,str3,str4.
line2 str1 = \"Java Programming\";
Here str1 will be created in string pool and points to Java Programming.
line 3 str2 = \"Java Programming\",, here str2 will be in string pool and points to the same string as where str1 points.
line4 str3 = new String(\"Java Programming\");
Here a string variable str3 gets created in heap memory and the holds value Java Programming.
line5 str4 = new String (\"Java Programming\");
Now one more string value created str4 in heap memory with different memory location holding value Java Programming.
