Char Array vs String: better to keep a set of letters

I need to save the code letter in constant class 4. I can do:

static final String CODE_LETTERS = "TRWAG"; 

or

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

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

 final char codeLetter = CODE_LETTERS.charAt(index); 

or

 final char codeLetter = CODE_LETTERS[index]; 

What is the best way ?. Keep in mind correction, performance, etc.

+7
java performance coding-style constants
source share
10 answers

In this case, performance does not matter. If it really needs to be constant, you cannot 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 } } 
+6
source share

Also not true, but since you would be dealing with char individually, I would personally use char [] . However, the impact this will have on productivity will be negligible, even if measured.

+8
source share

If you are going to receive a character several million times in a row, you do not need to worry about performance.

+6
source share

This is almost certainly premature optimization. No matter what you save in performance with an array of characters, readability may be lost if you need to pass it to other methods, since it is more canonical to accept String rather than char[] .

+5
source share

Strings are immutable, char [] is not. If you define this as a public β€œconstant” in a class, then String is a 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 vile and do this:

 MyClass.CODE_LETTERS[0] = 'Q'; 

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

The final keyword only affects an array reference; this does not apply to array elements. I see a similar error all the time with Collections.unmodifiableList() , people think they are protecting their list, but client code can still access and change the list items.

To answer your question, use String.

+5
source share

The String value really matches the char set. Thus, char[] as an implementation, although it does not mean a value, will not add an extra String value. OTOH, you can find a useful method in String . On the other hand, java.util.Arrays also has useful methods such as [binarySearch] [2].

Perhaps what you want to do is introduce an abstraction for the char set, which can vary the implementation between using String as the simplest one that can work, linear scanning char[] (fast, t scan very far), binary search, bit. sparse bit, hashing, stream filtering, etc.

[2]: http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#binarySearch (char [] , int, int, char)

+2
source share

Since the string uses char [] to store your messages, char [] is the correct answer. When you doubt the source, theres no magic in String, it just uses primitives like int and char [], like any other class.

You really should not care about something as trivial as this. The program has a lot of features that worry about whether one line is faster than using a char array.

+1
source share

The only time you see a performance difference between a String and an array of characters will be under the profiler, and then only because the profiler does the wrong thing. A modern JVMS (JDk 6+) will generate the same code for two calls as soon as the JVM decides that it is simple enough for optimization.

To answer your question; If you use Java 5, use enumerations; if you use something before Java5, use the Java enumeration pattern .

This will make your code more readable, since you will not need to track offsets anywhere, you can just use an enumeration. It will also be faster, since you can do something like:

 final char codeLetter = enum.getCodeLetter(); 
+1
source share

It looks like you should use enum . See Listing Types

+1
source share

Funny, I just wrote a blog post about this yesterday. You need a custom class wrapped around char []. My Characters class is an easy way to maintain an immutable character set and provides highly efficient methods for things like search. It is open source.

0
source share

All Articles