Created
March 12, 2022 09:35
-
-
Save geminicaprograms/2b5ac8fb60aecb609a405c086a6d65fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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