| 1. |
What will be the output of the below program? |
|
Answer» String str1 = "scaler"; //Line1String STR2 = new String("scaler"); //Line2str2.intern(); //Line 3System.out.println(str1 == str2); The output of the above program is false. We know that when the intern() method is executed or invoked on a string object, then it checks whether the String pool already has a same string value (scaler) or not, and if it is available, then the reference to the that string from the string constant pool is returned. In the above EXAMPLE, the intern method is invoked on str2. However, since we didn't assign it back to str2, str2 REMAINS UNCHANGED and therefore, both str1 and str2 have different references. If we change the code in line 3 to str2 = str2.intern(), then the output will be true. |
|