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

  • SEARCH
  • Home
  • 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 672795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:31:27+00:00 2026-05-14T00:31:27+00:00

I’m using enumerations to replace String constants in my java app (JRE 1.5). Is

  • 0

I’m using enumerations to replace String constants in my java app (JRE 1.5).

Is there a performance hit when I treat the enum as a static array of names in a method that is called constantly (e.g. when rendering the UI)?

My code looks a bit like this:

public String getValue(int col) {
  return ColumnValues.values()[col].toString();
}

Clarifications:

  • I’m concerned with a hidden cost related to enumerating values() repeatedly (e.g. inside paint() methods).
  • I can now see that all my scenarios include some int => enum conversion – which is not Java’s way.

What is the actual price of extracting the values() array? Is it even an issue?

Android developers

Read Simon Langhoff’s answer below, which has pointed out earlier by Geeks On Hugs in the accepted answer’s comments. Enum.values() must do a defensive copy

  • 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-14T00:31:27+00:00Added an answer on May 14, 2026 at 12:31 am

    Enum.values() gives you a reference to an array, and iterating over an array of enums costs the same as iterating over an array of strings. Meanwhile, comparing enum values to other enum values can actually be faster that comparing strings to strings.

    Meanwhile, if you’re worried about the cost of invoking the values() method versus already having a reference to the array, don’t worry. Method invocation in Java is (now) blazingly fast, and any time it actually matters to performance, the method invocation will be inlined by the compiler anyway.

    So, seriously, don’t worry about it. Concentrate on code readability instead, and use Enum so that the compiler will catch it if you ever try to use a constant value that your code wasn’t expecting to handle.


    If you’re curious about why enum comparisons might be faster than string comparisons, here are the details:

    It depends on whether the strings have been interned or not. For Enum objects, there is always only one instance of each enum value in the system, and so each call to Enum.equals() can be done very quickly, just as if you were using the == operator instead of the equals() method. In fact, with Enum objects, it’s safe to use == instead of equals(), whereas that’s not safe to do with strings.

    For strings, if the strings have been interned, then the comparison is just as fast as with an Enum. However, if the strings have not been interned, then the String.equals() method actually needs to walk the list of characters in both strings until either one of the strings ends or it discovers a character that is different between the two strings.

    But again, this likely doesn’t matter, even in Swing rendering code that must execute quickly. 🙂


    @Ben Lings points out that Enum.values() must do a defensive copy, since arrays are mutable and it’s possible you could replace a value in the array that is returned by Enum.values(). This means that you do have to consider the cost of that defensive copy. However, copying a single contiguous array is generally a fast operation, assuming that it is implemented “under the hood” using some kind of memory-copy call, rather than naively iterating over the elements in the array. So, I don’t think that changes the final answer here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use Uri.TryCreate to create a new Uri object only if… May 15, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer If they are files that your app downloads or creates,… May 15, 2026 at 3:50 pm
  • Editorial Team
    Editorial Team added an answer I think that if you are having problems with the… May 15, 2026 at 3:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.