mafu
mafu

Reputation: 32710

Is the char representation of a String distinct and fixed?

In Java, is it guaranteed that the toCharArray method always returns the same representation for strings that are considered equal?

I think not. I would argue the following:

Strings that are equal were not necessarily created from the same characters. For instance:

The Unicode code point for ö is U+00F6. Ö is U+00D6. It can also be created by compositing the character U+0308 "COMBINING DIAERESIS" with an "o" or "O". src

If a String were to return the char array it was created from (which may be different for Strings that compare equal), then toCharArray would return different values.

Apart from that, there are also Java version changes, leading to fixes or changes in the Unicode layer (this should be obvious).

Upvotes: 1

Views: 38

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533730

Just because they look the same, doesn't make them the same.

String o1 = "\u00F6";
String o2 = "o\u0308";
System.out.println(o1 + " equals to " + o2 + " is " + o1.equals(o2));
System.out.println(o1 + " compareTo " + o2 + " is " + o1.compareTo(o2));
System.out.println(o1 + " is " + Arrays.toString(o1.toCharArray()));
System.out.println(o2 + " is " + Arrays.toString(o2.toCharArray()));

prints

ö equals to ö is false
ö compareTo ö is 135
ö is [ö]
ö is [o, ̈]

or

ö equals to ö is false
ö compareTo ö is 135
ö is [ö]
ö is [o, ̈]

Upvotes: 4

Related Questions