Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7487841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:40:12+00:00 2026-05-29T14:40:12+00:00

When using flags in Java, I have seen two main approaches. One uses int

  • 0

When using flags in Java, I have seen two main approaches. One uses int values and a line of if-else statements. The other is to use enums and case-switch statements.

I was wondering if there was a difference in terms of memory usage and speed between using enums vs ints for flags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T14:40:14+00:00Added an answer on May 29, 2026 at 2:40 pm

    Both ints and enums can use both switch or if-then-else, and memory usage is also minimal for both, and speed is similar – there’s no significant difference between them on the points you raised.

    However, the most important difference is the type checking. Enums are checked, ints are not.

    Consider this code:

    public class SomeClass {
        public static int RED = 1;
        public static int BLUE = 2;
        public static int YELLOW = 3;
        public static int GREEN = 3; // sic
    
        private int color;
    
        public void setColor(int color) {
            this.color = color;
        }   
    }
    

    While many clients will use this properly,

    new SomeClass().setColor(SomeClass.RED);
    

    There is nothing stopping them from writing this:

    new SomeClass().setColor(999);
    

    There are three main problems with using the public static final pattern:

    • The problem occurs at runtime, not compile time, so it’s going to be more expensive to fix, and harder to find the cause
    • You have to write code to handle bad input – typically a if-then-else with a final else throw new IllegalArgumentException("Unknown color " + color); – again expensive
    • There is nothing preventing a collision of constants – the above class code will compile even though YELLOW and GREEN both have the same value 3

    If you use enums, you address all these problems:

    • Your code won’t compile unless you pass valid values in
    • No need for any special “bad input” code – the compiler handles that for you
    • Enum values are unique
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using java mail to automate Gmail operations. One of the operation
I have tried setting the debug flags using the set command in cmake but
I have a problem when generating java classes from WSDL using JAX-RPC wscompile ANT
We build software using Hudson and Maven. We have C#, java and last, but
Is it possible to set custom flags on IMAP mail messages using java mail
I have a java program that acts as a POP3 client using javax.mail. I
I have a Java process that opens a file using a FileReader. How can
I'm using Spring 3.x, Java 6. I have an @Around aspect with the following
I am new to Android and Java. I have constructed an app using HTML/Javascript
If I wanted to represent states or options or something similar using binary flags

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.