Back to the 2026 paper

Module 2: Arrays and Strings

20267m

Consider the following code segment:

String s1 = "Java";
String s2 = new String("Java");

Analyse the results of s1 == s2 and s1.equals(s2).

Worked SolutionAI Assisted

== versus .equals()

String s1 = "Java";
String s2 = new String("Java");
  • s1 == s2 is false because == compares object references, and new String(...) creates a distinct object.
  • s1.equals(s2) is true because String.equals() compares character contents.

For strings, use .equals() when comparing text. == is appropriate when reference identity is what you intend to test.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.