Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15942

This commit is contained in:
amit2103 2019-07-14 21:10:53 +05:30
commit 57c7b8bd97
4 changed files with 231 additions and 1 deletions

26
java-numbers-2/.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

135
java-numbers-2/pom.xml Normal file
View File

@ -0,0 +1,135 @@
<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>java-numbers-2</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-numbers-2</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.decimal4j</groupId>
<artifactId>decimal4j</artifactId>
<version>${decimal4j.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>java-numbers-2</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<commons-math3.version>3.6.1</commons-math3.version>
<decimal4j.version>1.0.3</decimal4j.version>
<commons-lang3.version>3.5</commons-lang3.version>
<assertj.version>3.6.1</assertj.version>
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,68 @@
package com.baeldung.lossyconversion;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
public class ConversionTechniquesUnitTest {
@Test
public void testPrimitiveConversion() {
long longNum = 24;
short shortNum = (short) longNum;
assertEquals(24, shortNum);
double doubleNum = 15.6;
int integerNum = (int) doubleNum;
assertEquals(15, integerNum);
long largeLongNum = 32768;
short minShortNum = (short) largeLongNum;
assertEquals(-32768, minShortNum);
long smallLongNum = -32769;
short maxShortNum = (short) smallLongNum;
assertEquals(32767, maxShortNum);
long maxLong = Long.MAX_VALUE;
int minInt = (int) maxLong;
assertEquals(-1, minInt);
long minLong = Long.MIN_VALUE;
int maxInt = (int) minLong;
assertEquals(0, maxInt);
}
@Test
public void testWrapperToPrimitiveConversion() {
Float floatNum = 17.564f;
long longNum = floatNum.longValue();
assertEquals(17, longNum);
Double doubleNum = 15.9999;
longNum = doubleNum.longValue();
assertEquals(15, longNum);
}
@Test
public void testWrapperToPrimitiveConversionUsingMathRound() {
Double doubleNum = 15.9999;
long longNum = Math.round(doubleNum);
assertEquals(16, longNum);
}
@Test
public void testWrapperConversion() {
Double doubleNum = 10.3;
double dbl = doubleNum.doubleValue(); //unboxing
int intgr = (int) dbl; //downcasting
Integer intNum = Integer.valueOf(intgr);
assertEquals(Integer.valueOf(10), intNum);
}
}

View File

@ -461,6 +461,7 @@
<!-- <module>java-ee-8-security-api</module> --> <!-- long running -->
<module>java-lite</module>
<module>java-numbers</module>
<module>java-numbers-2</module>
<module>java-rmi</module>
<module>java-spi</module>
<module>java-streams</module>
@ -987,7 +988,6 @@
<module>jws</module>
<module>libraries</module> <!-- very long running -->
<module>persistence-modules/hibernate5</module>
<module>persistence-modules/hibernate-mapping</module>
<module>persistence-modules/java-jpa</module>
@ -1147,6 +1147,7 @@
<module>java-ee-8-security-api</module>
<module>java-lite</module>
<module>java-numbers</module>
<module>java-numbers-2</module>
<module>java-rmi</module>
<module>java-spi</module>
<module>java-streams</module>