Skip to content

Instantly share code, notes, and snippets.

@geminicaprograms
Created March 12, 2022 09:35
Show Gist options
  • Save geminicaprograms/2b5ac8fb60aecb609a405c086a6d65fd to your computer and use it in GitHub Desktop.
Save geminicaprograms/2b5ac8fb60aecb609a405c086a6d65fd to your computer and use it in GitHub Desktop.
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class EqualsVsCompare {
private static boolean notCompares(Charset a, Charset b) {
return !(a == b) && (a == null || b == null || a.compareTo(b) != 0);
}
private static boolean notEquals(Charset a, Charset b) {
return !Objects.equals(a, b);
}
public static void main(String[] args) {
Charset one = null;
Charset two = StandardCharsets.UTF_8;
assert notCompares(one, two);
assert notCompares(two, one);
assert !notCompares(one, one);
assert !notCompares(two, two);
assert notCompares(two, StandardCharsets.UTF_16);
assert notEquals(one, two);
assert notEquals(two, one);
assert !notEquals(one, one);
assert !notEquals(two, two);
assert notEquals(two, StandardCharsets.UTF_16);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment