BAEL 5414 - Java null check why use == instead of .equals() (#12124)
This commit is contained in:
parent
b03e1b3216
commit
7e5463390b
5
core-java-modules/core-java-lang-5/README.md
Normal file
5
core-java-modules/core-java-lang-5/README.md
Normal file
@ -0,0 +1,5 @@
|
||||
## Core Java Lang (Part 5)
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
## TODO ##
|
27
core-java-modules/core-java-lang-5/pom.xml
Normal file
27
core-java-modules/core-java-lang-5/pom.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?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-lang-5</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-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>
|
||||
<finalName>core-java-lang-5</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,125 @@
|
||||
package com.baeldung.nullchecks;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class NullChecksUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenReferenceEqualityOnPrimitives_thenCompareValues() {
|
||||
int a = 10;
|
||||
int b = 15;
|
||||
int c = 10;
|
||||
int d = a;
|
||||
|
||||
// different values check
|
||||
assertFalse(a == b);
|
||||
|
||||
// same values check
|
||||
assertTrue(a == c);
|
||||
|
||||
// same references check
|
||||
assertTrue(a == d);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReferenceEqualityOnObjects_thenCompareReferences() {
|
||||
Person a = new Person("Bob", 20);
|
||||
Person b = new Person("Mike", 40);
|
||||
Person c = new Person("Bob", 20);
|
||||
Person d = a;
|
||||
Person e = null;
|
||||
|
||||
// different values check
|
||||
assertFalse(a == b);
|
||||
|
||||
// same values check
|
||||
assertFalse(a == c);
|
||||
|
||||
// same references check
|
||||
assertTrue(a == d);
|
||||
|
||||
// same references check - for nulls
|
||||
assertTrue(e == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueEqualityOnPrimitives_thenCompareValues() {
|
||||
int a = 10;
|
||||
Integer b = a;
|
||||
|
||||
assertTrue(b.equals(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenValueEqualityOnObjects_thenCompareValues() {
|
||||
Person a = new Person("Bob", 20);
|
||||
Person b = new Person("Mike", 40);
|
||||
Person c = new Person("Bob", 20);
|
||||
Person d = a;
|
||||
Person e = null;
|
||||
|
||||
// different values check
|
||||
assertFalse(a.equals(b));
|
||||
|
||||
// same values check
|
||||
assertTrue(a.equals(c));
|
||||
|
||||
// same references check
|
||||
assertTrue(a.equals(d));
|
||||
|
||||
// null checks
|
||||
assertFalse(a.equals(e));
|
||||
assertThrows(NullPointerException.class, () -> e.equals(a));
|
||||
|
||||
// null checks fixed
|
||||
assertFalse(e != null && e.equals(a));
|
||||
|
||||
// using Objects.equals
|
||||
assertFalse(Objects.equals(e, a));
|
||||
assertTrue(Objects.equals(null, e));
|
||||
|
||||
}
|
||||
|
||||
private class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Person person = (Person) o;
|
||||
return age == person.age && Objects.equals(name, person.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, age);
|
||||
}
|
||||
}
|
||||
}
|
@ -73,6 +73,7 @@
|
||||
<module>core-java-lang-2</module>
|
||||
<module>core-java-lang-3</module>
|
||||
<module>core-java-lang-4</module>
|
||||
<module>core-java-lang-5</module>
|
||||
<module>core-java-lang-math</module>
|
||||
<module>core-java-lang-math-2</module>
|
||||
<module>core-java-lang-math-3</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user