Syntax and exception occurrence conditions that can be used when comparing with null in Java

Behavior when null value is entered in equals method

a.equals(b) If a is null, an exception (NullPointException) is raised. If b is null, no exception is thrown and false is returned as the return value.

Object.equals(a,b) If either a or b is null, no exception will occur and false will be returned as the return value. If both a and b are null, teue is returned as the return value.

point

If you want to compare with null, use the equality operator (==).

Behavior when comparing empty strings ("")

• equals(b) • Object.equals(a,b) Either a or b can be compared with an empty string, and no exception occurs.

Equals method overload

You can compare instance objects of the same type with specific values in the field.

Reference site

Sample when the value to be compared by Java equals method is null or empty string

Recommended Posts

Syntax and exception occurrence conditions that can be used when comparing with null in Java
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
Summary of ORM "uroboroSQL" that can be used in enterprise Java
SwiftUI View that can be used in combination with other frameworks
Simple slot machine implementation that can be used with copy and paste
[Rails] "pry-rails" that can be used when saving with the create method
Performance analysis and failure diagnostic tools that can be used with OpenJDK
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
Technology excerpt that can be used for creating EC sites in Java training
Be careful with requests and responses when using the Serverless Framework in Java
Write a class that can be ordered in Java
[Java] When var should be used and when it should not be used
Convenient shortcut keys that can be used in Eclipse
Compiled kotlin with cli with docker and created an environment that can be executed with java
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Firebase-Realtime Database on Android that can be used with copy
Problems that can easily be mistaken for Java and JavaScript
Java (super beginner edition) that can be understood in 180 seconds
Object-oriented design that can be used when you want to return a response in form format
Reference memo / In-memory LDAP server that can be embedded in Java
Static analysis tool that can be used on GitHub [Java version]
Note that system properties including JAXBContext cannot be used in Java11
Introduction to Java that can be understood even with Krillin (Part 1)
Syntax examples often used in Java
[Java 8] Until converting standard input that can be used in coding tests into a list or array
Java file input / output processing that can be used through historical background
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Ruby array methods that can be used with Rails (other than each)
[Swift] Color Picker that can be used with copy and paste (palette that allows you to freely select colors)
[Java] Precautions when comparing character strings with character strings
Create KeyStore and sign apk in processing (android mode) (java can be used if some changes are made)
Check with Java / Kotlin that files cannot be written in UAC on Windows
Questions in java exception handling throw and try-catch
Encrypt / decrypt with AES256 in PHP and Java
The story that the port can no longer be used in the Spring boot sample program
Error logging and exception handling that you can't often see in the Java area
Let's create a TODO application in Java 11 Exception handling when accessing TODO with a non-existent ID