Java Basic Syntax

  • Facebook
  • Google
  • LinkedIn
  • Twitter

The basic syntax of java is some key points, applicable on all java programs, which you need to remember and implement according to syntax :

Java is case sensitive

Java is case sensitive means it differentiate between same words but having different cases i.e. upper case and lower case. For example : 'Java' and 'java' are different words in java.

Class name Convention :

According to java convention, the first letter of program's name should be in upper case. All other letters can be in any case. The rule is not strictly applied , but you have to follow the convention for better implementation. The program also runs if your program name starts with lower case letter.

See the below example, the program name starts with upper case but it also run if your program name start with lower case :

First Java Program:

Let us look at a simple code that would print the words Hello World.

public class MyFirstJavaProgram {

/* This is my first java program.  
* This will print 'Hello World' as the output
*/

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

compile and run the program.

Output:
Hello World

Method naming Convention:

According to java convention, the first letter of the method name start with lower case. The rule is not strictly applied, but you have to follow the convention for better implementation.

Program File Name:

Name of the program file should exactly match the class name. When saving the file you should save it using the class name (Remember java is case sensitive) and append '.java' to the end of the name. (if the file name and the class name do not match your program will not compile). Example : Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java'

public static void main(String args[]):

java program processing starts from the main() method which is a mandatory part of every java program.

Java Identifiers:

All java components require names. Names used for classes, variables and methods are called identifiers.

In java there are several points to remember about identifiers. They are as follows:

  • All identifiers should begin with a letter (A to Z or a to z ), currency character ($) or an underscore (_).

  • After the first character identifiers can have any combination of characters.

  • A key word cannot be used as an identifier.

  • Most importantly identifiers are case sensitive.

  • Examples of legal identifiers:age, $salary, _value, __1_value

  • Examples of illegal identifiers : 345abc, -salary

Java Keywords:

The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.

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