• Follow Us

Basic Syntax Of Java

In this section of the Java tutorial, we will discuss the Basic Syntax of Java Programming language. A Java program can be considered as a collection of objects that communicate via invoking each other’s methods.

Let us discuss now briefly how a class looks, what are objects, variables and more!

  • Object

An object in Java is like a combination of data and procedures. Every object has a state and behavior. Example: A cow has states – color, name, and behavior such as eating. The state of an object is stored in variables and function displays the behavior of objects.  

  • Class

A class is like a context which is used to create objects and defines objects state and behavior.

  • Methods

Methods are a set of codes in which all technical codes or logics are written and executed. Each method has its own name and returns a value as well.

  • Instance Variables

Instance variables are used by objects to store their states. Each object has its own state and unique instance variable.

First Java Program

Let us take a simples code that will print the words Hello World.

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

Learn how to save the file, compile, and run the program.

Please follow the steps −

  • Open notepad and add the code as written above.
  • Save the file as: JavaProgram.java.
  • Open the command prompt window and go to the directory where you have saved  . Assume it’s D:\.
  • Type ‘javac JavaProgram.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 (  The path variable is set).
  • Now, type ‘ java JavaProgram ‘ to run your program.
  • You will be able to see ‘ Hely Dear ‘ printed on the window.

Output

D:\> javac MyFirstJavaProgram.java

D:\> java MyFirstJavaProgram

Hey Dear class

Java Programs, the Basic syntax is very important to keep in mind

  • Case Sensitivity − Java is case sensitive programing language, which means identifier Hey and hey would have a different meaning in Java.
  • Class Names −First letter of all class names should be in Upper Case and it will be same for all words if you are using one than one-word name.
    Example: class FirstJavaProgram
  • Method Names − All method names will start with a Lower Case letter and if you are using one than a one-word name, all inner word’s first letter will be Capital.


Example: public void firstJavaProgram()

  • Program File Name − Name of the program file should exactly match the class name.

    When you save the file, you need to save it using the class name (Remember Java is case sensitive) and use extension ‘.java’ to the end of the file name (if the file name and the class name do not match, your program will not compile).

    Also,  make a note that in some specific cases you do not have a public class present in the file then file name can be different than class name. It is also not compulsory to have a public class in the file.

    Example: Suppose ‘FirstJavaProgram’ is the class name. Then the file should be saved as ‘FirstJavaProgram.java’
  • public static void main(String args[]) − Execution of Java program starts from the main() method which is a compulsory part of every Java program.

Java Identifiers

The components we use in Java needs a name for referring them from which they can be identified. Those names are called Identifiers in Java.

Here are some important terms about identifier names. Which you need to know

  • Names of all Identifiers must be started from (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character, identifiers can have any combination of characters.
  • Keywords can’t be used as an Identifiers.
  • Identifiers are also case sensitive.
  • Examples of legal identifiers: age, $salary, _value, __1_value.
  • Examples of illegal identifiers: 123abc, -salary.

Java Modifiers

Using modifiers we can edit classes, methods, etc in Java. There are two types of modifiers

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

Java Variables

There are 3 types of Java Variables

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

Java Arrays

Java array is those types of objects which can contain similar data type. We can only store a fixed set of elements in Java Arrays and the first element of the array is stored at 0 index.

Java Enums

Enums are served from the purpose of representing predefined values. The values in this enumerated list are called enums. By using of enums in code saves from bugs.

For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium, and large. This would make sure that it would not allow anyone to order any size other than small, medium, or large.

Example

 
 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 −

Output

Size: MEDIUM
Note − Enums can be declared as their own or inside a class. Methods, variables, constructors can be defined inside enums as well.

Java Keywords

These are few predefined words in Java which can’t be used as constant or variable or any other identifier names.

abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfpsuper
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile

Comments in Java

Java also supports single-line and multi-line comments very similar to C and C++. All characters inside any comment are ignored by Java compiler.

Example

 
 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");
    }
 } 

Output

Hello World

Using Blank Lines

A line which contains only white spaces, and comes up with a comment, is known as a blank line, and Java compiler ignores it.

Inheritance

In Java, classes can be derived from other classes. Basically, you can drive a new class for the existing class and it’s called Inheritance.

With this concept, we can inherit fields and methods from a parent class and reuse them instead of making a new class. In the procedure, 1st build class is known as Parent Class and inherited class is known as Child Class

Interfaces

Interfaces in Java can be defined as an interconnection between objects on how they communicate. Interfaces are most used when it comes to inheritance.

An interface defines the methods, which a child class use. But, implementation of methods depends on the parent class.

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart