September 24, 2009

Java keywords , Identifiers and comments

As we are talking about Java over here. So, have you all ever tried to look carefully at any Java program? What all things it consist of? Well, it consists of a collection of white space, identifiers, literals, comments, operators, separators and keywords. These are the basic categories in which a Java program can be categorised from the angle of parsing it. Few of them vital ones I am discussing in this post. Rest I will try to cover in separate post. If not able to do so, no worries they are self-explanatory. :)

Java Keywords: Currently there are 50 reserved words in Java language. These keywords cannot be used as names of a variable, class or method but they can be combined with other characters or symbols to be used as the name of any variable, class or method.
[
Note: 
1. const and goto are reserved words in Java language but are not used. 
2. true, false and null are not part of Java keywords list but still they are also reserved words in Java language.
]
Updated list of Java Keywords as of version 6 is following:
Java Keywords :

  1. abstract 
  2. assert
  3. boolean
  4. break
  5. byte
  6. case
  7. catch
  8. char
  9. class
  10. const
  11. continue
  12. default
  13. do
  14. double
  15. else
  16. enum
  17. extends
  18. final
  19. finally
  20. float
  21. for
  22. goto
  23. if
  24. implements
  25. improt
  26. instanceof
  27. int
  28. interface
  29. long
  30. native
  31. new
  32. package
  33. private
  34. protected
  35. public
  36. return
  37. short
  38. static
  39. strictfp
  40. super
  41. switch
  42. synchronized
  43. this
  44. throw
  45. throws
  46. transient
  47. try
  48. void
  49. volatile
  50. while
Identifiers: Identifiers are used for names of class, variables or methods. Following is a few set of points that one should keep in mind while using identifiers.

  • Identifiers can begin with a letter, an underscore, or a currency character.
  • After the first character, identifiers can contain any combination of letters, currency characters, connecting chracters, or numbers.
  • Identifiers can be of any length.
  • Identifiers are case-sensitive.eg. foo and Foo are two different identifiers.
  • Java keywords(for list of Java keywords refer any Java book) cannot be used as identifier.
Few of the legal identifiers are :
int _a;
int this_is_a_very_detailed_name_for_an_identifier;
int _$;
int ________2_w;
int $c;


Few of the illegal identifiers are :
int :b;
int .f;
int 7g;
int -d;
int e#
*;

[*Note: According to java language specification, a java identifier can contain only A-Z, a-z, 0-9, _ and $, and start only with one of a-z,A-Z, or $.# is not one of these more over, the identifier rule says "After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers".]


Comments: Basically there are three types of comments used in Java language.


1. single-line comment : It is used to comment only one-single line. These comments start with // at the starting of the code line to be commented.
Example: // System.out.println("Hello"); 


2. multi-line comment : It is used to comment more than one line. These comments start with /* at starting code line and ends with */ at the ending code line.
Example: 
/*public static void main(String[] args){
System.out.println("Hello");
}*/


3. documentation comment : Generally it is used to produce an HTML file that documents our program. These comments start with /** and ends with */ and all in between lines are prefixed with * at the starting.
Example:
/**
 * This method prints "Hello" 
 * @author shalinig
 */

public static void main(String[] args){
System.out.println("Hello");
}


Feel free to fire questions! :)