What happens at compile-time and running of the following code?

public class Test {
  public static void main(String[] args) {
    String s1 = new String("string");
    String s2 = new String("STRINg");
        
    StringBuilder sb1 = new StringBuilder("test");
    StringBuilder sb2 = new StringBuilder("test");
        
    System.out.println(s1.equalsIgnoreCase(s2) && sb1.equals(sb2)
        && s2.charAt(s2.length()) == 'g');
  }
}
Explanation
The method equalsIgnoreCase() returns true since during such comparison the register is not case sensitive.
The method equals() in the StringBuilder and StringBuffer is not overridden and does not allow the comparison so it always returns false.
Thanks to && the further check is not performed, thus it allows to avoid StringIndexOutOfBoundsException, as the latest available element in the string can be [s2.length()-1].

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты