How many times does the following code print true?
1: public class Main { 2: public static void main(String[] args) { 3: String main = "main"; 4: System.out.println(main.toUpperCase() == main); 5: System.out.println(main.toUpperCase() == main.toUpperCase()); 6: System.out.println(main.toUpperCase().equals(main)); 7: System.out.println(main.toUpperCase().equals(main.toUpperCase())); 8: System.out.println(main.toUpperCase().equalsIgnoreCase(main)); 9: System.out.println(main.toUpperCase() 10: .equalsIgnoreCase(main.toUpperCase())); 11: } /* w ww .ja v a2s. co m*/ 12:}
C.
Lines 4 and 5 both print false since a String should be compared with a method rather than ==, especially when not comparing two values from the string pool.
Line 6 also prints false because one value is uppercase and the other is lowercase.
Line 7 prints true because both values are uppercase.
Lines 8 and 9 print true because they don't look at the case.
Option C is the answer.