Java Jornal: Legal Identifiers
66
By definition, Identifier is a unique expression in a written format either by a code, by numbers or by the combination of both to distinguish variations from one to another among a class of substances, items, or objects.
As far as I know about Java and its legal identifiers, it should
technically contain only Unicode characters, numbers, currency symbols,
and connecting characters. Other than that, there are certain rules and tips on how we should be able effectively create or design better legal identifier in Java. See rules below which are essentially a Java programmer should know.
- The first character of the identifier should start either with a letter, a currency character ( $ ), or a connecting character like underscore( _ ).
- Identifiers that starts with a number is illegal.
- The first character maybe followed by either or a combination of letters, currency characters, connection characters, or numbers.
- There is no limit of how many characters an identifier can contain. However, it's essentially recommended to use a short character identifier yet readable and understandable.
- Using Java keywords as an identifier is illegal. In other words, you can't use a Java keyword as an identifier.
- More importantly, keep in mind that Java Identifier are case sensitive. Thereby, NAME and name are two different identifiers.
Legal identifiers examples:
String _t; String _$; String _2; String _____2_u; String $s; String this_is_a_long_sample_of_identifier_name; String ALL_UPPERCASE; String all_lowercase;
Illegal identifiers examples:
String :c; String -b; String s#; String .d; String 9n; String ?what; String boo+!coo;
Developers should follow certain standards in naming identifiers essentially because cost of maintenance relly heavily on reading their legacy codes. According to Sun Microsystem, "Over the lifetime of a piece of, 20% of the effort will go into the original and testing of the code while the 80% of the effort will go into the subsequent maintainance and enhancement of the code.".
My point is that, make your code maintenance easy by essentially following proper naming convention and coding standards. This way you'll reduce cost of maintenance and making it easy for future developers to enhance the software technically. For this, I highly recommend you to visit java.sun.com and look for a document titled "Java Code Conventions."
Here are some hint on how to formulate indentifier name:
- Classes & Interface - The first letter should be capitalized. If the desired name is composed of several words, the first letter of the inner word should be capitalized (camelCase). For example: Chair, InformationDepartment and HeadOffice.
- Methods - The first letter should be lowecase, and then camelCase rules should be applied. Also, take note that in method names should be verb-noun pairs. For example: getTotal, setContactNumber and buyCellphone.
- Variables - same rules apply on letter case with methods. However, it should idially be a short and meaningful. For example: firstName, currentLocation and totalPrice.
- Constants - Java constants should be named using uppercase letters with underscore characters as separator. Take note that constants are created by marking variables as static and final. For example: PERSON_LABEL, HELLO_MESSAGE and GREETING.
Finally, Im done! Hope this article helps you. For more details about this topic, please do visit java.sun.com. Stay tune for more java journal...
PrintShare it! — Rate it: up down flag this hub









