BAEL-5390: Compare Characters in Java (#12633)

* BAEL-5390: Compare Characters in Java

* create new module core-java-string-operations-5
This commit is contained in:
Azhwani 2022-08-28 18:53:06 +02:00 committed by GitHub
parent 98c81c1d08
commit 15581547df
3 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,2 @@
### Relevant Articles:

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-5</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-string-operations-5</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,55 @@
package com.baeldung.charcomparison;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Objects;
import org.junit.jupiter.api.Test;
class CharComparisonUnitTest {
@Test
void givenTwoPrimitiveChars_whenUsingRelationalOperators_thenCompare() {
assertFalse('a' == 'A');
assertTrue('a' < 'v');
assertTrue('F' > 'D');
}
@Test
void givenTwoPrimitiveChars_whenUsingCompareMethod_thenCompare() {
assertTrue(Character.compare('C', 'C') == 0);
assertTrue(Character.compare('f', 'A') > 0);
assertTrue(Character.compare('Y', 'z') < 0);
}
@Test
void givenTwoCharObjects_whenUsingCompareToMethod_thenCompare() {
Character chK = Character.valueOf('K');
Character chG = Character.valueOf('G');
Character chH = Character.valueOf('H');
assertTrue(chK.compareTo(chK) == 0);
assertTrue(chK.compareTo(chG) > 0);
assertTrue(chG.compareTo(chH) < 0);
}
@Test
void givenTwoCharObjects_whenUsingEqualsMethod_thenCompare() {
Character chL = 'L';
Character chV = 'V';
assertTrue(chL.equals(chL));
assertFalse(chL.equals(chV));
}
@Test
void givenTwoCharObjects_whenUsingObjectsEqualsMethod_thenCompare() {
Character chA = 'A';
Character chB = 'B';
assertTrue(Objects.equals(chA, chA));
assertFalse(Objects.equals(chA, chB));
}
}