When to use a constant?

Below is a code snippet of ArrayList, which either takes in the 'initialCapacity' provided by the user or default of 10. My question - why was 10 not a static constant ? I guess constants are used when a variable is repeated more than once in the code. Am I right ?

public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
}

/**
 * Constructs an empty list with an initial capacity of ten.
 */
public ArrayList() {
    this(10);
}
Answer:1

Constants are used to give names to variables.

You could call it "DEFAULT_INITIAL_CAPACITY"

a good program, is one that you can read like a story.

Here is a code snippet for example:

char * find_type(char *str,int *type){
    str = trim_leading_space(str);
    if(*str == SLASH_SIGN){
        str++;
    }else{
        display_error(BAD_TYPE_FORMAT, str);
    }
    str = trim_leading_space(str);
    *type = convert_to_zero_or_one(str[0]);
    if(*type == NO){
        display_error(BAD_TYPE_FORMAT, str);
    }else {
        str++;
    }

    return str;
}

the code is written in C, but still proves my point.

here is the definition of "BAD_TYPE_FORMAT"

#define BAD_TYPE_FORMAT 34

if I had left it as 34, when someone came to edit my code, he wouldn't know why i wrote exactly 34 in it. But it was just the next index of the error, from a long list of possible errors.

Answer:2

Constants are their to make your code more readable. To Java it just don't matters whether you declare any thing as constant or not. Once the code is compiled, every constant will be concatenated (not sure if you can use that term here) and if its an expression that can be evaluated at compile time, it will be.

So if you write

 

    int INITIAL_CAPACITY  = 10;

    public ArrayList() {
        this(INITIAL_CAPACITY);
    }

after compilation will always be converted to

 

    public ArrayList() {
        this(10);
    }

This is all done to save time at run time. So its better to declare constants separately as that makes it more human readable as it states clearly to user what its intended for. In your case, the INITIAL_CAPACITY make more sense to someone not familiar with the code then just '10' used in the call.

You don't have to bother about these optimization as JVM will do that for you. You should try to make more readable.

Answer:3

There is a constant for this starting with Java 7.

In the code example you post the value is used only once, and a comment describes what it's for, so there is no point in using a constant. It could be argued it's better off without a constant because when reading you don't have to stop and look around for the constant definition. Using a constant creates an expectation in the reader that this is something that will show up multiple times, hard-coding this makes it clear that this value doesn't appear anywhere else. For these reasons not using a constant could have been a choice made to improve readability.

Once the initialization logic changed to be lazy, the default capacity value had to be referenced in two places (because it had to be checked when adding a new element). Introducing a constant made sense; if a maintainer of the class wants to change the initial capacity then they only have to make the change in one place.

Answer:4

If I have a regular expression, how do I return the substring that it has found? I'm sure I must be missing something obvious, but I've found various methods to confirm that that substring is ...

While I do understand some of the corner-cases of generics, I'm missing something with the following example. I have the following class 1 public class Test<T> { 2 public static void main(...

My android app needs communicate with server when it's (application) opened. For that I want to use socketIO Java client. Do I need to use BoundService for sockets (it should run on background)? ...

I was poking around the rabbitmq documentation, and it seems that rabbitmq does not handle message redelivery count. If I were to manually ACK/NACK messages, I would need to either keep the retry ...