Skip to content

Commit 809460c

Browse files
committed
[int-to-short] int to short
1 parent 887697d commit 809460c

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Relevant Articles:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-conversions.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>core-java-numbers-conversions-2</artifactId>
6+
<packaging>jar</packaging>
7+
<name>core-java-numbers-conversions-2</name>
8+
9+
<parent>
10+
<groupId>com.baeldung.core-java-modules</groupId>
11+
<artifactId>core-java-modules</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
</parent>
14+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.inttoshort;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ConvertIntToShortUnitTest {
8+
9+
@Test
10+
void whenUsingCasting_thenCorrect() {
11+
short expected = 42;
12+
int i = 42;
13+
short result = (short) i;
14+
assertEquals(expected, result);
15+
}
16+
17+
@Test
18+
void whenUsingIntegerShortValue_thenCorrect() {
19+
short expected = 42;
20+
Integer intObj = 42;
21+
short result = intObj.shortValue();
22+
assertEquals(expected, result);
23+
}
24+
25+
@Test
26+
void whenIntOutOfShortRange_thenGetTheUnexpectedResult() {
27+
int oneMillion = 1_000_000;
28+
short result = (short) oneMillion;
29+
assertEquals(16960, result);
30+
31+
int twoMillion = 2_000_000;
32+
result = (short) twoMillion;
33+
assertEquals(-31616, result);
34+
}
35+
36+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<!-- <module>core-java-modules/core-java-18</module> --> <!-- JAVA-26056 -->
3737
<!-- <module>core-java-modules/core-java-19</module> --> <!-- JAVA-26056 -->
3838
<module>core-java-numbers-conversions</module>
39+
<module>core-java-numbers-conversions-2</module>
3940
<module>core-java-9-improvements</module>
4041
<module>core-java-9-streams</module>
4142
<module>core-java-9</module>

0 commit comments

Comments
 (0)