How to avoid exceptions with Java's equals method

When I somehow investigated how to use equals at work, I found a higher-grade usage, so I will talk about it. I already know this! Please look at it with warm eyes. And please point out any mistakes.

equals method

I think the equals method is often used when comparing strings of the String class. At that time, I used to call it like this.

equals.java


str.equals("hoge");

However, with this call method, if str is null, equals will be called from null, so a *** nullPointerException exception *** will occur.

As a way to avoid this exception, it is ant to check for null with an if statement, but you can avoid the exception by changing the equals method a little.

That's here.

equals.java


"hoge".equals(str);

It's easy. It's just the opposite of str and "hoge". This call returns false instead of nullPointerException. Therefore, the *** nullPointerException exception *** will not occur.

bonus

This time I introduced the equals method of the String class, but there is also the equals method of the Object class.

equals.java


Objects.equals(str1,str2);

This explanation will be on another occasion.

Finally

From now on, when using the equals method, I want to call the string first.

Recommended Posts

How to avoid exceptions with Java's equals method
[Java] How to compare with equals method
How to number (number) with html.erb
How to update with activerecord-import
How to create a method
How to handle exceptions coolly with Java 8 Stream or Optional
How to use the link_to method
How to scroll horizontally with ScrollView
How to use the include? method
How to use the form_with method
How to get started with slim
[Java] How to use join method
Introduction to algorithms with java-Shakutori method
How to use mssql-tools with alpine
How to use Ruby inject method
How to start Camunda with Docker
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Android] How to deal with dark themes
[Rails] How to use rails console with docker
How to use submit method (Java Silver)
How to switch thumbnail images with JavaScript
[Rails] How to use the map method
[Note] How to get started with Rspec
[Java] How to use the toString () method
How to have params in link_to method
How to do API-based control with cancancan
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
[Rails] How to use helper method, confimartion
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
[Rails] How to operate the helper method used in the main application with Administrate
How to download Oracle JDK 8 rpm with curl
Be careful to avoid access blocking with scraping
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
Override protected method with anonymous class to stub
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to test interrupts during Thread.sleep with JUnit
How to search multiple columns with gem ransack
[Ruby] How to use gsub method and sub method
How to use Java framework with AWS Lambda! ??
[Swift] How to link the app with Firebase
How to create multiple pull-down menus with ActiveHash
How to use Java API with lambda expression
How to get started with Eclipse Micro Profile
How to insert all at once with MyBatis
How to use the replace () method (Java Silver)
How to monitor SPA site transitions with WKWebView
How to write test code with Basic authentication