Java Basic Concepts

In this tutorial, you will learn and understand the basic concepts of Java. Understanding the basic concepts of Java provides a solid foundation for learning and using the language effectively. It allows you to grasp the core principles and syntax of Java, which are building blocks for more advanced topics.

Basic Code Syntax

The code syntax in a programming language is a set of rules for writing code. Java syntax is mostly derived from C and C++ programming language.

In Java, all code is written inside the body of a class. A class body is defined with the opening and closing curly braces { }.

A class is a template for creating objects. A class may contain fields, methods, constructors, and nested classes.

Here is a simple Java code that will display the phrase "Hello world" in the console:

package com.example;

public class Sample {

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

}

Let's break down the above code:

  • package com.example: This line declares the package name (com.example). The package statement specifies the package where the Java class belongs. It helps organize classes into logical groups or namespaces.
  • public class Sample: This line declares the class named "Sample". The public keyword indicates that the class is accessible from other classes. The class name "Sample" is defined here.
  • public static void main(String[] args): This line declares the main method. The public keyword indicates that the main method can be accessed from outside the class. The static keyword means that the method belongs to the class itself, rather than an instance of the class. The main method serves as the entry point for the program.
  • System.out.println("Hello world"): This line prints "Hello world". This statement uses the System.out.println() method to display the text "Hello world" in the console.
  • The code structure follows a class-oriented approach. It declares a class named "Sample" with a main method, which is where the program execution begins. Inside the main method, the "Hello world" message is printed to the console using System.out.println().

    It is important to note that a Java class name must always match the file name, else the program will fail to compile. A Java file has .java extension.


    Comments in Java

    Comments in Java, are statements that are ignored by the compiler and the interpreter. Comments are basically used to make the code more readable by providing information about a method, variable, class, etc. There are two types of comments in Java:

    1. Single Line comment - It starts with a double forward slash "//".
    2. MultiLine comment - It starts with /* and ends with */.
    public class CommentsExample {
    
      // This is a single line comment.
    
      /*
       *This is a multiline comment.
       *Compiler ignores this section of the code.
       */
    
    }

    Semicolon in Java

    In Java, a semicolon is used to separate statements and therefore, every statement must end with a semicolon (;).

    private String message = "Hello! how are you?";
    System.out.println("Message = " + message);

    Identifiers in Java

    An identifier is the name used for identifying a package, class, interface, method, or variable in the code. An identifier in Java is case-sensitive. A standard naming conventions should be followed when selecting names. An identifier must be composed of letters, numbers, the underscore _ and the dollar sign $. Identifiers must always begin with a letter, the underscore or a dollar sign.

    An identifier should be named in such a way that makes their purpose obvious. Here is an example of some variables that hold the name, profession, and department of an employee:

    //The class name Employee is identifier here
    public class Employee {
    
      //The name, profession, and department is identifier here
      String name = "Peter";
      String profession = "programmer";
      String department = "IT";
    
      //This is a method where main is identifier here
      public static void main(String args []) {
        System.out.printf("My name is %s. I am a %s. My department is %.",
        name, profession, department);
       }
    }

    The output of the above code will be the following:

    My name is Peter. I am a programmer. My department is IT.


    Java is case-sensitive

    Java is case-sensitive which means it distinguishes between uppercase and lowercase letters when using identifiers such as variable names, method names, and class names.

    Here are a few examples to illustrate case sensitivity in Java:

    The following code declares two different variables:

    int count = 70;
    int Count = 90;

    If we create two methods with the same name but different cases, Java treats them as separate methods:

    public void displayMessage() {
       System.out.println("Hello world!");
    }
    
    public void DisplayMessage() {
       System.out.println("Hello Universe!");
    }

    Java class names are also case-sensitive. The following code defines two different classes:

    public class MyClass {
    
    }
    
    public class Myclass {
    
    }

    Modifiers in Java

    A modifier in Java, modifies the properties of classes, constructors, methods or variables. There are two types of modifiers in Java:

    • Access modifiers — The access modifiers are used to restrict the scope of a class, constructor, method, variable or data member. There are four types of access modifiers in Java. They are the default, private, protected, and public. Here is an example of a method with a public access modifier which can be accessed from anywhere in the program:
    • public int substract(int a, int b) {
          int remaining = a - b;
          return remaining;
      }
    • Non-access modifiers — Java provides a group of non-access modifiers such as final, static, abstract, volatile, transient, synchronized, etc. The use of non-access modifiers gives special properties to variables, methods, and classes. However, they do not change the accessibility of variables, methods, and classes. Here is an example of a class and a method with the abstract non-access modifier:
    • public abstract class Car {
         public abstract double speed();
      }

      Main Method in Java

      The main method is a special method that serves as the entry point for a Java application. It is the starting point from which the Java Virtual Machine (JVM) begins to execute a Java program.

      The main method must have the following signature:

      public static void main(String[] args)

      Let's break down the parts of the main method signature:

      • public - It specifies that the method can be accessed from anywhere in the program.
      • static - It indicates that the main method belongs to the class itself rather than an instance of the class. This allows the JVM to call the main method without creating an object of the class.
      • void - It indicates that the method does not return any value.
      • main - It is the name of the method. The JVM looks for a method with this name to start the execution of the program.
      • string [] args - This is the parameter of the main method. It is an array of strings that allows you to pass command-line arguments to your program.

      Here's a simple example of a Java program with a main method:

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

      In the above code, the main method simply prints the string "Hello world!" to the console when the program is executed

      Keywords in Java

      Keywords are reserved words which have predefined functions in Java. Keywords cannot be used as names for classes, variables, methods, or as any other identifier. There are around 57 reserved words in Java.

      Here is the list of keywords available for use in Java:

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