Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in
Sequence
== or equals?
Core exercise
Complete the two methods: avecEgalEgal compares with ==, avecEquals compares with equals. Run the main: the two strings contain the same text, and yet the two methods do not give the same answer. Explain why in a comment.
A workshop goes here: you write and run code in it. It requires JavaScript.
Starting code
Exercices.java
public class Exercices {
public static void main(String[] args) {
String a = new String("java");
String b = new String("java");
System.out.println("avec == : " + avecEgalEgal(a, b));
System.out.println("avec equals : " + avecEquals(a, b));
// TODO : expliquer la difference ici, en commentaire
}
public static boolean avecEgalEgal(String a, String b) {
// TODO : comparer avec ==
return false;
}
public static boolean avecEquals(String a, String b) {
// TODO : comparer avec equals
return false;
}
}
The core exercise is validated in the workshop, which requires JavaScript.
Hint 1
== asks “is it the same memory cell?”. equals asks “is it the same contents?”. Identical string literals are shared by Java, but new String("java") does create a distinct object.