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
Зареєструйся Зараз
або Підпишись на майбутні тести