This is a simple but frustratingly time consuming question. In the above code, only two string objects are created (not three). String s3 = new String("hello") //new string object is created in the heap area. String s2 = "hello" //no new object is created because the same String already exists in the string pool So, for example, String s1 = "hello" //new interned string object containing hello is created When you create a string using the new operator, interned strings are not used and a new String object is created. If the string exists, then it uses the same String object instead of creating a new one. a string within double quotes), the JVM checks whether that string already exists in the pool or not. ![]() The JVM maintains a pool of all the String objects. You should remember the following rules about this topic: It is not mentioned explicitly in exam objectives but a few candidates have reported seeing such questions in the exam. Note: String interning is a complex topic with many nuances.
0 Comments
Leave a Reply. |