Java Basic Syntax

For Java programmer, it is very important to keep in mind about the following points.

  • Case Sensitivity - Java is a case sensitive language, which means that the identifier Hello, hello, HeLLo, hEllO, helLo, HELLO. All are different in Java.
  • Method Names - All the method names should start with a Lower Case letter.
    If several words are used to form the name of the method, then each first letter of inner word should be in Upper Case
    Example: public void employeeRecords(), public void myMethodName(), public void employeeNumber() etc.
  • Class Names - For all class names, the first letter should be in Uppercase
    If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
    Examples: class MyJavaProgram, class MyThirdJavaProgram, class StudentRecords etc.
  • public static void main(String args[]): Java program processing starts from the method main() which is a mandatory part of every Java program.
  • Program File Name - Name of a program file should exactly match the class name.
    When saving the file, you should save it using the class name and add/append '.java' to end of the name.
    If file name and the class name do not match, your program will not compile.
    Example: Think 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'

Java Basic Syntax Example

Following is Java's simplest program, prints Hello World on the screen.

/* Java Program Example - Java Basic Syntax */

public class JavaProgram
{
    public static void main(String args[])
    {
        /* print Hello World */
        
        System.out.println("Hello World");
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java basic syntax

Identifiers in Java

In Java, an identifiers are used to name things, such as classes, variables, and methods.

An identifier may be any sequence of uppercase and lowercase letters, number or the underscore and dollar-sign characters. As you know, Java is case-sensitive, therefore, VALUE is a different identifier than Value.

Following are the rules to declare Identifiers in Java:

  • All identifiers can begin with a letter (A to Z or a to z) or dollar currency character ($) or an underscore (_).
  • After the first character identifiers can have any combination of characters.
  • Most importantly identifiers are case-sensitive.
  • A keyword cannot be used as an identifier since it has reserved words and have some special meaning.
  • Examples of illegal identifiers: 123abc, -salary etc.
  • Examples of legal identifiers: AvgTemp, count, a4, this_is_ok, age, $salary, _value, __1_value, customers etc.

Whitespaces in Java

A line containing only whitespace, possibly with comment, is known as blank line, and Java compiler totally ignores it.

Comments in Java

There are the three types of comments defined by Java. You have already seen two: single-line and multiline comments. The third type is a documentation comment. This type of comment is used to produce an HTML file that documents your program. The documentation comment begins with /** and ends with */.

All characters available inside any comment are ignored by Java compiler. Her is an example.

/* Java Program Example - Java Basic Syntax */

public class JavaProgram
{
    
    /* 
   This is a simple Java program.
   It will print 'Hello World' on the output screen
   It is an example of multi-line comments or block comments.
    */
   
    public static void main(String args[])
    {
        
        // It is an example of single line comment
   /* It is also an example of single line comment. */
      
   System.out.println("Hello World"); 
        
    }
}

When the above Java program is compile and run, it will produce the following output:

comments in java

Java Online Test


« Previous Tutorial Next Tutorial »