.equals() ?

Patrick Kelly
1 min readMar 10, 2018

Consider this java code:

if (o.getApplyType().name().equals(ApplyType.NULL)) ...

If you look carefully at this java code, you’ll see that the condition will never be true. o.getApplyType().name() is clearly going to be a String. ApplyType.NULL is not ever going to be a String. This condition will never be true.

Java is commonly thought of as “strongly typed”, but this is an example of where it could be more strongly typed. Why even try to check if two things are equal when they are different types?

Go has stronger type checking:

type Meters int
type Feet int
func main() {
var foo Meters = 42
var goo Feet = 42
if foo == goo {
fmt.Println("they are equal!")
}
}

This program will produce an error:

invalid operation: foo == goo (mismatched types Meters and Feet)

Even though we know, and the compiler knows, that these are int underneath, the language/compiler is here to help us, and does so by telling us that you cannot check things of different types for equality.

Sometimes, go may seem too strict, but really it’s better that way.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Patrick Kelly
Patrick Kelly

Written by Patrick Kelly

Web/database engineer/gopher. Cycling, photos, yada, yada.

No responses yet

Write a response