Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Mini tutorial] Comparing enums (the right way)

Featured Replies

An enum type member is implicitly static and final, it will always reference the same object.

 

I never ever want to see enum comparison with the equals method again. Use "==".

 

Let's create a simple Animal enum:

enum Animal {

DOG, CAT, MOUSE;

}

And a Cage class:

class Cage {

private final Animal animal;

public Cage(final Animal animal) {
this.animal = animal;
}

public final Animal getAnimal() {
return animal;
}

}

The following will thrown a null pointer exception:

Since the first cage's animal is null, calling the equals() method on it will throw an exception!

new Cage(null).getAnimal().equals(new Cage(Animal.DOG).getAnimal()); 

The following will return false:

The first cage's animal is still null, however "==" is not a class method but an operator that allows null on both sides.

You are not calling a method on null, therefore no exception will be thrown

new Cage(null).getAnimal() == new Cage(Animal.DOG).getAnimal();

The following will return true: 

new Cage(Animal.DOG).getAnimal().equals(new Cage(Animal.DOG).getAnimal());

The following will also return true:

An enum type member is implicitly static and final, it will always reference the same object.

You can safely use the "==" operator! AND ALWAYS SHOULD :)

new Cage(Animal.DOG).getAnimal() == new Cage(Animal.DOG).getAnimal();

Edited by Botre

  • 5 weeks later...

Your justification is convincing, but I'm not comfortable running comparisons against non-primitive/non-wrapped-primitive-object types. Maybe I'll break the habit. smile.png

Edited by liverare

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.