I was trying to get the equality of 'ü' == 'ü'. This returns true if I write like this (in sout). But in my code:
static Character[] charset = {'ü'};
public static void main(String[] args) {
ArrayList<Character> mustContain = new ArrayList<>();
mustContain.add('ü');
ArrayList<Character> removal = new ArrayList<>();
for (int i = 0; i < charset.length; i++) {
for (int j = 0; j < mustContain.size(); j++) {
if(charset[i] == 'ü' && mustContain.get(j) == 'ü') {
System.out.println(charset[i] instanceof Character);
System.out.println(mustContain.get(j) instanceof Character);
}
if(charset[i] == (mustContain.get(j))){
System.out.println("added");
removal.add(mustContain.get(j));
}
}
}
System.out.println(Arrays.toString(removal.toArray()));
}
This is the result:
true
true
[]
What I hope to get was:
true
true
added
[ü]
I can only get this equality as true with charset[i].equals(mustContain.get(j))
But for the Character 'a' everything is okay with the code, both == and .equals() returns true. For 'ü' only .equals() works. I really wonder why equals works and == not?