Home >>Java Tutorial >Java Basic Syntax

Java Basic Syntax

Java Basic Syntax

While considering a Java program, defining it becomes an easy task and it can be defined as a collection of objects that generally communicate via invoking the methods of each other. Here is a brief description of class, object, methods, and instance variables:

  • Object − Objects are known to possesses states and behaviors. For instance: A human being has states - color, name, and behavior and emotions. An object is also known as the instance of the class.
  • Class − It can be basically defined as a template/blueprint that is used to describe the behavior/state that is being supported by the objects.
  • Methods − A behavior of anything is simply known as a Method. Many methods can be contained in a class. The methods are responsible for the manipulation of written logics and execution of all the actions.
  • Instance Variables − Every object has its own unique set of instance variables. And the state of an object is generally created by the values that are assigned to these instance variables.

Les's See First Java Program

A simple Java code to print the words Welcome user

public class FirstJavaProgram 
{

    /*This is my very first program in Java.
    * This Prorgram will print 'Welcome user'*/

   public static void main(String []args) 
   {
      System.out.println("Welcome user"); // prints Welcome user
   }
}

Here is how one can save the file, compile, and run the program of the Java language.
Here are the steps that depict the procedure to perform the code with the syntax:

  • First step is to open the notepad and add the code as mentioned above.
  • Now you have to save the file as: FirstJavaProgram.java.
  • After this step, you have to open a command prompt window and reach to the directory where the class has been saved by you. Suppose it's C:\.
  • Now you have to type 'javac FirstJavaProgram.java' and then hit the enter button to compile code. It will be very good if there are no errors in your code. After this the command prompt will redirect you to the next line. Please make sure that the path variable is set.
  • After these step, you'll have to type ' java FirstJavaProgram ' in order to run your program.
  • When your program will be executed then you will be able to see ' Welcome user ' printed on the window.
Output : Welcome user

Basic Syntax of Java

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

The below mentioned points are the most important that should be kept in mind about Java programs:

  • Case Sensitivity − Java is known to be case sensitive that basically means identifier Hello and hello will have a very different meaning in the Java language.
  • Class Names − The first letter should be in the Upper Case for all the name of the classes. Each of the inner word's first letter should be in the Upper Case in the case that several words are being used to create the name of the class.
  • Method Names − Lower Case letter should be used to start the name of the methods in Java. Each inner word's first letter should be in Upper Case if there are numerous words that are used to form the name of the methods.
  • Program File Name – The name of the program file should be an exact match of the class name.

The file should be saved as the class name and please remember that the Java is case sensitive while saving the file. And you should append '.java' to the end of the name please note that the program will not compile if the file name and the class name do not match.

There is a catch in this procedure that is, in case you do not have a public class that is present in the file then the file name could be different than the class name. Having a public class in the file is generally not mandatory.

Example: Let's suppose that 'MyJavaProgram' is the name of the class then it is mandatory that the file should be saved as 'MyJavaProgram.java'

  • public static void main(String args[]) - The processing of the program of the Java generally starts from the main() method that is a mandatory part of each and every Java program.

No Sidebar ads