Skip to content

Commit c9e8ca2

Browse files
authored
BAEL 5414 - Java null check why use == instead of .equals() (eugenp#12124)
1 parent 3682c97 commit c9e8ca2

4 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Core Java Lang (Part 5)
2+
3+
This module contains articles about core features in the Java language
4+
5+
## TODO ##
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-lang-5</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java-lang-5</name>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.baeldung.core-java-modules</groupId>
13+
<artifactId>core-java-modules</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
</parent>
16+
17+
<build>
18+
<finalName>core-java-lang-5</finalName>
19+
<resources>
20+
<resource>
21+
<directory>src/main/resources</directory>
22+
<filtering>true</filtering>
23+
</resource>
24+
</resources>
25+
</build>
26+
27+
</project>
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.baeldung.nullchecks;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Objects;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class NullChecksUnitTest {
10+
11+
@Test
12+
public void whenReferenceEqualityOnPrimitives_thenCompareValues() {
13+
int a = 10;
14+
int b = 15;
15+
int c = 10;
16+
int d = a;
17+
18+
// different values check
19+
assertFalse(a == b);
20+
21+
// same values check
22+
assertTrue(a == c);
23+
24+
// same references check
25+
assertTrue(a == d);
26+
}
27+
28+
@Test
29+
public void whenReferenceEqualityOnObjects_thenCompareReferences() {
30+
Person a = new Person("Bob", 20);
31+
Person b = new Person("Mike", 40);
32+
Person c = new Person("Bob", 20);
33+
Person d = a;
34+
Person e = null;
35+
36+
// different values check
37+
assertFalse(a == b);
38+
39+
// same values check
40+
assertFalse(a == c);
41+
42+
// same references check
43+
assertTrue(a == d);
44+
45+
// same references check - for nulls
46+
assertTrue(e == null);
47+
}
48+
49+
@Test
50+
public void whenValueEqualityOnPrimitives_thenCompareValues() {
51+
int a = 10;
52+
Integer b = a;
53+
54+
assertTrue(b.equals(10));
55+
}
56+
57+
@Test
58+
public void whenValueEqualityOnObjects_thenCompareValues() {
59+
Person a = new Person("Bob", 20);
60+
Person b = new Person("Mike", 40);
61+
Person c = new Person("Bob", 20);
62+
Person d = a;
63+
Person e = null;
64+
65+
// different values check
66+
assertFalse(a.equals(b));
67+
68+
// same values check
69+
assertTrue(a.equals(c));
70+
71+
// same references check
72+
assertTrue(a.equals(d));
73+
74+
// null checks
75+
assertFalse(a.equals(e));
76+
assertThrows(NullPointerException.class, () -> e.equals(a));
77+
78+
// null checks fixed
79+
assertFalse(e != null && e.equals(a));
80+
81+
// using Objects.equals
82+
assertFalse(Objects.equals(e, a));
83+
assertTrue(Objects.equals(null, e));
84+
85+
}
86+
87+
private class Person {
88+
private String name;
89+
private int age;
90+
91+
public Person(String name, int age) {
92+
this.name = name;
93+
this.age = age;
94+
}
95+
96+
public String getName() {
97+
return name;
98+
}
99+
100+
public void setName(String name) {
101+
this.name = name;
102+
}
103+
104+
public int getAge() {
105+
return age;
106+
}
107+
108+
public void setAge(int age) {
109+
this.age = age;
110+
}
111+
112+
@Override
113+
public boolean equals(Object o) {
114+
if (this == o) return true;
115+
if (o == null || getClass() != o.getClass()) return false;
116+
Person person = (Person) o;
117+
return age == person.age && Objects.equals(name, person.name);
118+
}
119+
120+
@Override
121+
public int hashCode() {
122+
return Objects.hash(name, age);
123+
}
124+
}
125+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<module>core-java-lang-2</module>
7474
<module>core-java-lang-3</module>
7575
<module>core-java-lang-4</module>
76+
<module>core-java-lang-5</module>
7677
<module>core-java-lang-math</module>
7778
<module>core-java-lang-math-2</module>
7879
<module>core-java-lang-math-3</module>

0 commit comments

Comments
 (0)