9/30/15

Java Keywords, identifiers and Naming conventions


Hi Readers,

Knowing about java keywords in java coding is very important. This comes under java for beginners.

So we will cover two aspects of Java identifiers


Legal identifiers

There are some rules that compiler uses to determine valid java identifiers. In very technical language legal identifier must be consisting of only Unicode characters, numbers, currency symbols and connecting characters.
But you need not to worry about unicode. Here are some rules you do need to know
  1. Java identifier must start with a letter, currency symbols($) or connecting characters( _ ). Identifiers can not start with a number.
  2. After first character, identifier can contain any combination of letters, currency symbols, connecting characters or numbers.
  3. In java language there is no limit on how long your identifier should be.
  4. You can not use java keywords as identifiers.
  5. You can not have other special symbols like " : " in your identifier name.
  6. Java identifiers are case sensitive. So name and Name are different.
Java keyword list is given below. This list is updated to java version 8.

abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while

Example of some legal identifiers

int  $a;
int  _b;
int  ____c;
int  _$;
int  this_is_best_example_of_java_identifiers

Example of some illegal identifiers

int  :a
int  -b

Coding conventions

Java has coding conventions for a reason. From the very beginning softwares were known to have 20% time of their lifecycle in coding and 80% time in maintenance. So taking some coding guidelines and standards into consideration reduces effort involved in testing, bug fixing and enhancing that piece of code. So Sun has created a set of coding standards and published under a document named "Java Code Conventions". Here is what Sun has recommended.


Classes and interfaces

First letter should be Capitalised.
If there are more than one word. Then all the words will start with First letter Uppercase ( this is called camelCase)
For classes names should typically be nouns. For Example :-
Cat
CompanyAccount


Methods

First letter should be lowercase and then follow camelCase. Name should typically be a verb-noun pair. For Example :-
getBalance
doApply


Variables

Like methods, camelCase format with first letter lowercase. Examples
screenWidth
accountBalance


Constants

Constants are declared static and final. They named should be all uppercase with underscore as separators.
APP_VERSION
JAVA_HOME