Char Array vs String: which is better for storing a set of letters

13,686

Solution 1

Performance is irrelevant in this case. If it's really supposed to be constant, you can't use the char[] approach. Consider:

public class Test
{
  static final char[] CODE_LETTERS = {'T', 'R', 'W', 'A', 'G'};

  public static void main(String[] args) throws Exception
  {
    System.out.println(CODE_LETTERS[0]); // T
    CODE_LETTERS[0] = 'x';
    System.out.println(CODE_LETTERS[0]); // x
  }
}

Solution 2

Neither is incorrect, but since you're going to be dealing with the chars individually I'd personally use the char []. That said, the impact this will have on performance is going to be negligible if even measurable.

Solution 3

Strings are immutable, char[] is not. If you are defining this as a public "constant" in a class then String is the real constant.

For example if you have this:

public class MyClass { 
    public static final char[] CODE_LETTERS = {'h', 'e', 'l', 'l', 'o'};
    ....
}

I can be all sneaky and do this:

MyClass.CODE_LETTERS[0] = 'Q';

Bam, I've changed the value of your "constant".

The final keyword only affects the reference to the array, it does not apply to the array elements. I see a similar mistake all the time with Collections.unmodifiableList(), people think its protecting their list but client code can still access and modify the list elements.

So to answer your question, use the String.

Solution 4

Unless you are going to fetch the character several million times in a row, you don't need to bother about the performance.

Solution 5

This is almost certainly a premature optimization. Whatever you save in performance by using a character array may be lost in readability if you need to give it to other methods, since it's more canonical to accept a String rather than a char[].

Share:
13,686
xgomez
Author by

xgomez

Developer passionate about agile methodologies.

Updated on July 26, 2022

Comments

  • xgomez
    xgomez almost 2 years

    I need to store in a constant class 4 letter of a code. I can do:

    static final String CODE_LETTERS = "TRWAG";
    

    or

    static final char[] CODE_LETTERS = {'T', 'R', 'W', 'A', 'G'};
    

    After, I can obtain one of that characters in two ways:

    final char codeLetter = CODE_LETTERS.charAt(index);
    

    or

    final char codeLetter = CODE_LETTERS[index];
    

    what is the best way?. Please take in mind correction, performance, etc.

  • pyrocumulus
    pyrocumulus about 15 years
    My thoughts exactly. Seriously: Premature optimization is the root of all evil.
  • PaulJWilliams
    PaulJWilliams about 15 years
    But equally one should not pessimize prematurely. For a fixed length immutable string a char array will always be quicker and have less memory overhead than the equiavlent std::string object.
  • Brett
    Brett about 15 years
    Who says performance is irrelevant? Lots of people spend a lot of effort on performance. Of course the implementation can do naughty things, but if the representation is localised we can just say "don't do that then".
  • Brett
    Brett about 15 years
    It may well be the sort of thing that gets done lots of times. What if this was a new method in the implementation of java.lang.String?
  • Alan Moore
    Alan Moore about 15 years
    I meant it's not a question of performance, because the char[] version doesn't even work as intended. Let's concentrate on correcting the obvious errors IN THIS CASE: premature optimization, and using an array as a constant.
  • DJClayworth
    DJClayworth about 15 years
    Without knowing what the use is that's not a good suggestion. If he is going to compare the character against other characters then an enum doesn't make sense.
  • DJClayworth
    DJClayworth about 15 years
    There's no indication that enum is what he wants. If he is going to be using the characters as characters somewhere then it doesn't make sense to store them as enums.
  • Vishy
    Vishy about 15 years
    If the enum values are T, R, W, A, G then toString() could be used.
  • Alan Moore
    Alan Moore about 15 years
    I edited my answer after the exchange above and added the "in this case" qualifier. I'm mentioning it here to avoid confusing readers too badly.
  • orad
    orad over 12 years
    Yes, but when a string is semantically meant to be used as a string instead of an array of characters then String should be used. Using char[] in Java only makes sense if you want to work with individual character items or need to frequently replace array positions.
  • ahcox
    ahcox about 12 years
    Your class would have sorted his characters on construction so it doesn't really answer his question, but I like the Idea behind the class. If I could +1 your blog post in its own right I would.
  • Garret Wilson
    Garret Wilson about 12 years
    @ahcox, thanks for the nice words. I've added Facebook "like" capability to my blog site, so feel free to try that out! ;)