Comments
There are threee types of comments in Java.
Single line Comment
1 | // System.out.println("This is an comment."); |
Multi-line Comment
1 2 3 | /* System.out.println("This is the first line comment."); System.out.println("This is the second line comment."); */ |
Documentation Comment. Also called a doc comment
1 | /** documentation */ |
Source file name
The name of a source file should exactly match the public class name with the extension of .java. The name of the file can be a different name if it does not have any public class.
Case sensitivity
Java is a case-sensitive language, which means that the identifiers AB, Ab, aB, ******and ab are different in Java.
1 2 | System.out.println("Wrapline"); // valid syntax system.out.println("Wrapline"); // invalid syntax because of the first letter of System keyword is always uppercase. |
Class name
- The first letter of the class should be in Uppercase (lowercase is allowed but discouraged).
- If several words are used to form the name of the class, each inner word’s first letter should be in Uppercase. Underscores are allowed, but not recommended. Also allowed are numbers and currency symbols, although the latter are also discouraged because they are used for a special purpose (for inner and anonymous classes).
1 2 3 4 5 6 | class MyJavaProgram // valid syntax class 1Program // invalid syntax class My1Program // valid syntax class $Program // valid syntax, but discouraged class My$Program // valid syntax, but discouraged (inner class Program inside the class My) class myJavaProgram // valid syntax, but discouraged |
public static void main(String [] args)
The method main() is the main entry point into a Java program; this is where the processing starts. Also allowed is the signature public static void main(String… args).
Method name
- All the method names should start with a lowercase letter (uppercase is also allowed but lowercase is recommended).
- If several words are used to form the name of the method, then each first letter of the inner word should be in Uppercase. Underscores are allowed, but not recommended. Also allowed are digits and currency symbols.
1 2 | public void employeeRecords() // valid syntax public void EmployeeRecords() // valid syntax, but discouraged |
Identifiers
- All identifiers can begin with a letter, a currency symbol or an underscore (_). According to the convention, a letter should be lower case for variables.
- The first character of identifiers can be followed by any combination of letters, digits, currency symbols and the underscore. The underscore is not recommended for the names of variables. Constants (static final attributes and enums) should be in all Uppercase letters.
- Most importantly identifiers are case-sensitive.
- A keyword cannot be used as an identifier since it is a reserved word and has some special meaning.
1 2 | Legal identifiers: MinNumber, total, ak74, hello_world, $amount, _under_value Illegal identifiers: 74ak, -amount |
White spaces
A line containing only white spaces, possibly with the comment, is known as a blank line, and the Java compiler totally ignores it.
Access Modifiers
These modifiers control the scope of class and methods.
- Access Modifiers: default, public, protected, private.
- Non-access Modifiers: final, abstract, static, transient, synchronized, volatile, native.
Keywords
abstract | do | import | short | volatile | synchronized |
byte | extends | long | switch | while | static |
class | for | private | throws | transient | protected |
native | double | boolean | finally | try | void |
instanceof | const | catch | if | this | throw |
goto | case | continue | int | strictfp | super |
final | assert | else | new | pubilc | return |
package | default | char | interface | enum | break |
implements | float |