Problem statement:
what will be output and explain why?
what will be output and explain why?
- public class ExecutionFive {
- public static void main(String[] args) {
- String s1 = "Cat";
- String s2 = "Cat";
- System.out.println(s1 == s2);
- }
- }
Output:
true
Explanation - whenever we create an object of String class using 'string literal', string literal means using double quotation mark(" "), then that object stores in a place called string constant pool of heap. So, what JVM does, it checks the object in the string constant pool area, if that object is not available there, then it stores the new object. if the object is already available in string constant pool, then instead of creating new object, it points to the existing object, in this way we are saving the space, time and ultimately good performance.
Below is the memory diagram for string constant pool
Below is the memory diagram for string constant pool
No comments:
Post a Comment