Fill in appropriately.String st1 = new String("JAVA"); String st2 = new String("JAVA");String st3="JAVA" objects, in heap memory and in string pool
Question
Fill in appropriately.String st1 = new String("JAVA"); String st2 = new String("JAVA");String st3="JAVA" objects, in heap memory and in string pool
Solution
In Java, when you create a String object using the new keyword, it always creates a new object in heap memory. If the object is created without the new keyword, it may return an existing object from the string pool, if it already exists. Otherwise, it will create a new string object in the string pool.
So, in your case:
String st1 = new String("JAVA");This will create a new object in the heap memory.String st2 = new String("JAVA");This will create another new object in the heap memory.String st3 = "JAVA";This will not create a new object. Instead, it will check the string pool and return the reference of the already existing "JAVA" object. If "JAVA" was not in the string pool, it would create a new object there.
So, in total, you have 2 objects in heap memory (st1 and st2) and 1 object in the string pool (st3).
Similar Questions
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace() .
Which class in Java provides a mutable alternative to String for efficient string manipulations?This question is required.*AStringBuilderBMutableStringCStringListDStringBuffer
ct answerWhat will s2 contain after following lines of Java code?StringBuffer s1 = "Ram";StringBuffer s2 = s1.append(" Ali")
Which of the following is NOT a valid way to declare and initialize a string variable in Java?Question 5Answera.String str = "Hello";b.String str = new String("Hello");c.String str = 'Hello';d.char[ ] charArray = {'H', 'e', 'l', 'l', 'o'};String str = new String(charArray);
Consider the following code,string s=new String(); will create an instance of string with
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.