Java "ANSI constants"
Asked Answered
C

3

28

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.

Courante answered 4/10, 2013 at 6:29 Comment(1)
this is an interesting question. I'm curious about your thoughts on my answer. Also, I happen to work at Oracle and I could check if any of the original authors are still here if you're after a more authoritative answer.Ebro
E
8

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).

Ebro answered 25/3, 2015 at 21:22 Comment(3)
Thanks for your answer, but it's really only speculation; I was hoping for a more authoritative answer. Also, to me it seems like a fair bit of stretch to say that someone would refer to "C" as "ANSI", but who knows. :)Courante
Right. I did find an older version of the document which (accidentally?) has C-code in it (right above the ANSI constants part). So I wouldn't say stretch to C is that far :-) But I'm curious about this too, and I'll try to reach out to the authors. Will update.Ebro
1: LENGTH is a preprocessor symbol, i.e. a macro. By the time the compiler sees the input "LENGTH" will have been replaced with the string "10". LENGTH is not allocated any storage at runtime. 'length' is the name given to a region of fixed storage at runtime. 2: Any updates? 3: I suspect that the reference to "ANSI constants" is a copy/paste error that should be removed from the document. It seems that nobody can account for its presence.Esther
A
7

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.

Aliunde answered 4/10, 2013 at 6:46 Comment(1)
"It's possible that the author was referring to string literals." -- Not likely. Java string literals are not ASCII, they are UTF-16.Ebro
P
0

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. ASCII the numeric encoding that maps the value to the character representation ie 65 = A, is covered by an ANSI standard. NaN (not a number) is a value that can be return by floating point calculations, and that value is defined by ANSI in conjuction with IEEE. Constants are established for a lot of things by ANSI especially in areas of communications such as SQL, Character Sets, colors ( thats why black is a value of 0 on most computers!) etc. So if you define a constant that represents a constant defined in an ANSI standard such as public static final ANSI_BLACK = 0; you should follow these conventions. (Notice Sun violates these conventions a lot as with NaN, Color.black etc.)

I Hope This Helps Carl Trusiak, SCJP2, SCWCD

Using these may cause internal conflicts while debugging as there may be wrong references to the user-defined constants or the implied value of the constant.

Perplexity answered 3/11, 2020 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.