Skip to content

Commit 8cabe50

Browse files
authored
Merge pull request #16479 from sk1418/int-to-short
[int-to-short] int to short
2 parents 77e15d2 + 7b3b52c commit 8cabe50

4 files changed

Lines changed: 71 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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.inttoshort;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class ConvertIntToShortUnitTest {
9+
10+
@Test
11+
void whenUsingCasting_thenCorrect() {
12+
short expected = 42;
13+
int i = 42;
14+
short result = (short) i;
15+
assertEquals(expected, result);
16+
}
17+
18+
@Test
19+
void whenUsingIntegerShortValue_thenCorrect() {
20+
short expected = 42;
21+
Integer intObj = 42;
22+
short result = intObj.shortValue();
23+
assertEquals(expected, result);
24+
}
25+
26+
@Test
27+
void whenIntOutOfShortRange_thenGetTheUnexpectedResult() {
28+
int oneMillion = 1_000_000;
29+
short result = (short) oneMillion;
30+
assertEquals(16960, result);
31+
32+
int twoMillion = 2_000_000;
33+
result = (short) twoMillion;
34+
assertEquals(-31616, result);
35+
}
36+
37+
short intToShort(int i) {
38+
if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) {
39+
throw new IllegalArgumentException("Int is out of short range");
40+
}
41+
return (short) i;
42+
}
43+
44+
@Test
45+
void whenCheckShortRangeBeforeCasting_thenGetExpectedResult() {
46+
short expected = 42;
47+
int int42 = 42;
48+
assertEquals(expected, intToShort(int42));
49+
50+
int oneMillion = 1_000_000;
51+
assertThrows(IllegalArgumentException.class, () -> intToShort(oneMillion));
52+
53+
}
54+
55+
}

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)