Categories > Etc > Software & Hardware >

Java equals() selection

UlaDeia

Senior Developer

Posts: 14

Threads: 9

Joined: Jul, 2022

Reputation: 4

Posted

In Java, if I try to do.equals() on a null string, a null pointer error is issued. I'm wondering whether I can perform the following if I'm attempting to compare if a string is equal to a constant string:

 

MY CONSTANT STRING.equals(aStringVariable)

I'm sure it'll work, but is this simply extremely bad code?

This is a common Java idiom known colloquially as a Yoda condition. Personally, I prefer to handle the null situation directly, but the Yoda method is widely used, and any competent Java programmer should quickly grasp what is going on. How should I proceed?

  • 0

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

life is too short to code in java bro

  • 1

i use arch btw

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

It's not bad code, just a simple string check lol

 

  • 0

Random quote here...

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )