public class Exercise {
public static void main(String[] args)
{
// Create three strings in three different ways.
String str1 = "Java Exercises";
String str2 = new StringBuffer("Java").append(" Exercises").toString();
String str3 = str2.intern();
// Determine which strings are equivalent using the ==
// operator (as compared to calling equals(), which is
// a more expensive operation.
System.out.println("str1 == str2? " + (str1 == str2));
System.out.println("str1 == str3? " + (str1 == str3));
}
}
str1 == str2? false
str1 == str3? true
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.