Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "ANSI constants"

Tags:

I hit this little tidbit while browsing the Java Code Conventions:

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

(From here.)

What are these "ANSI constants" this document speaks of? And how do they make debugging harder?

The text makes it sound as if there is a dichotomy between "variables declared class constants" (which I interpret as ordinary static final variables) and these "ANSI constants", but I'm not aware of any way to declare constants in Java other than to make them static final variables.

like image 464
Dolda2000 Avatar asked Oct 04 '13 06:10

Dolda2000


People also ask

What are ANSI constants?

Here, ANSI Constants refer to the predefined constants defined by the ANSI (American National Standards Institute). Please, refer to this link for more details. ANSI is actually an orginization the establishes standards that makes life easier on all of us. There are ANSI standards established for a lot of things.

Should constants be capitalized in Java?

The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.)

What would it mean if the name of a variable was spelled in all uppercase Java?

Using ALL uppercase letters are primarily used to identify constant variables. Remember that variable names are case-sensitive. You cannot use a java keyword (reserved word) for a variable name.


2 Answers

The only references on the internet on what are ANSI constants are in forums where people who have read the naming conventions ask the same question. It seems that the term was invented by the person who wrote the document and you would have to ask them what they meant.

ANSI is a national standards body in the USA, known for example for the ASCII character set standard and the ANSI C language standard. ANSI is also what Microsoft Windows calls the regional ASCII-based default character encoding. It's possible that the author was referring to string literals.

like image 121
Joni Avatar answered Sep 19 '22 12:09

Joni


They are most likely referring to ANSI C constants, defined as follows

In ANSI C constants can be defined two ways: through the #define statement and through use of the const modifier. For example, the following two statement, are equivalent:

#define LENGTH 10       /* Length of the square in inches */  const int length = 10;  /* Length of the square in inches */ 

Now there are obviously no C constants in Java, because Java is not C :-) (that's not the strangest part of the official coding conventions, but I digress).

So why did they write ANSI constants? This is most likely just a convenient way of referring to final primitives and immutable objects. Remember that in C the fields of a const struct can't be updated after initialization, and there's no corresponding Java term for such "constant" (final doesn't capture the notion of immutability very well).

like image 31
aioobe Avatar answered Sep 21 '22 12:09

aioobe