Java Basic Syntax


While we believe a Java program, it can be described as a compilation of objects that converse via invoking every other's techniques. Let us currently briefly see what class, object, techniques, and example variables signify.

  • Object − Objects have positions and performances. Example: A dog has positions - color, name, breed as well as actions such as wagging their end, barking, eating. An object is an example of a class.
  • Class − A class can be described as a template/blueprint that depicts the behavior/state that the object of its kind sustains.
  • Methods − A process is essentially a deed. A class can enclose numerous processes. It is in the process where the logic is written, data is control and every action is completed.
  • Instance Variables − each object has its sole set of example variables. An object's state is marked by the values allocated to these example variables.

First Java Program

Let us seem at an easy code that will print the utterances 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"); // prints Hello World

Let's look at how to save the file, compile, and run the program. Please follow the subsequent steps −

  • Open notepad and add the code as above.
  • Save the file as: MyFirstJavaProgram.java.
  • Open a command prompt window and go to the directory where you saved the class. Assume it's C:
  • Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set).
  • Now, type ' java MyFirstJavaProgram ' to run your program.
  • You will be able to see ' Hello World ' printed on the window.


C:\> javac MyFirstJavaProgram.java
C:\> java MyFirstJavaProgram 
Hello World

Basic Syntax

About Java programs, it is extremely significant to remember the following points.

  • Case Sensitivity − Java is case sensitive, which stands for identifier Hello and hello would have dissimilar sense in Java.
  • Class Names − for every one class name the primary letter should be in Upper Case. If more than a few words are employed to shape a name of the class, every inner word's initial letter should be in Upper Case.
  • Example: class MyFirstJavaClass
  • Method Names − All method names should create with a Lower Case letter. If numerous words are employed to shape the name of the technique, then each internal word's first letter should be in Upper Case.
  • Example: public void myMethodName()
  • Program File Name − The name of the program file should precisely match the class name.

When saving the file, you should save it by the class name (Remember Java is case sensitive) and append '.java' to the end of the name.

However, please create a note that lest you do not possess a public class here in the file then the file name can be diverse than the class name. It is as well not compulsory to have a public class in the file.

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 constituents necessitate names. Names utilized for classes, variables, and methods are recognized identifiers.

  • In Java, there are more than a few points to keep in mind 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 keyword 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: 123abc, -salary.

Java Modifiers

Like other programming languages, it is likely to adapt classes, methods, etc., with modifiers. There are two categories of modifiers −

  • Access Modifiers − default, public, protected, private
  • Non-access Modifiers − final, abstract, strictfp

We will be searching into more features about modifiers in the next section.

Java Variables

Following are the sorts of variables in Java

  • Local Variables
  • Class Variables (Static Variables)
  • Instance Variables (Non-static Variables)

Java Arrays

Selections are objects that lay up multiple variables of the equivalent type. However, an arrangement itself is an object on the bundle. We will appear into how to announce, construct, and initialize in the future chapters.

Java Enums

Enums were set up in Java 5.0. Enums limit a variable to contain one of only a few predefined values. The values in this detailed list are called enums.

With the apply of enums, it is the potential to lessen the number of bugs in your code.

For instance, if we think of an application for a new juice shop, it would be likely to limit the glass size to little, medium, and large. This would confirm that it would not let anyone individually any size other than a minute, medium, or large.


class FreshJuice {
   enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
   FreshJuiceSize size;

public class FreshJuiceTest {

   public static void main(String args[]) {
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
      System.out.println("Size: " + juice.size);

The above example will produce the following result:



Note − Enums can be stated as possessors within a class. Methods, variables, constructors can be described inside enums as well.

Java Keywords

The subsequent list explains the reserved words in Java. These hold-back words may not be employed as steady, variable, or any other identifier names.


Comments in Java

Java hold up single-line and multi-line remarks very alike to C and C++. Every character obtainable inside any comment is disregard by Java compiler.


public class MyFirstJavaProgram {

   /* This is my first java program.
    * This will print 'Hello World' as the output
    * This is an example of multi-line comments.

   public static void main(String []args) {
      // This is an example of single line comment
      /* This is also an example of single line comment. */
      System.out.println("Hello World");


Hello World

Using Blank Lines

A line containing only white space, perhaps with a comment, is acknowledged as a blank line, and Java absolutely overlooks it.


In Java, classes can be obtained from classes. If you require creating a new class, then it is potential to obtain your new class from the by now offered code.

This concept permits you to use again the meadows and techniques of the obtainable class with no have to rewrite the code in an original class. In this situation, the obtainable class is called the superclass and the get class is called the subclass.


In Java language, a line can be characterized as a contract between objects on how to converse with each other. Crossing points play an essential role while it comes to the idea of legacy.

A border defines the methods, a derived class (subclass) should utilize. However, the execution of the systems is entirely up to the subclass.