Java is a general-purpose programming language. It was designed to be versatile and can be used for a wide range of applications, from web development to mobile apps and enterprise software.

What is Java?

  • Sun Microsystems released Java programming language in 1995.
  • It is considered as simple, portable, secure and robust.
  • Java is a platform-independent language. It means that Java program/code written on one platform (ex: Windows OS), can be run on another platform (ex: MacOS).
  • Java is a general purpose, high-level programming language & supports object-oriented programming.
  • It can be used for a wide range of applications, from web development to mobile apps and enterprise software.

JDK, JRE and JVM

JDK, JRE and JVM in Java
JDK, JRE and JVM
  • JDK stands for Java Development Kit, JRE stands for Java Run-time Environment and JVM stands for Java Virtual Machine.
  • Once you install JDK, it also adds JVM on your machine.
  • JVM makes your Java program platform-independent. It converts bytecode into machine code.
  • JRE is a package that provides JVM plus Java class libraries.
  • In the end, JDK is required to develop applications in JAVA. It contains JRE, Compiler and Debugger.

Object-Oriented Programming in Java

Java is an object-oriented programming (OOP) language that provides a clear structure for organizing and managing code. The OOP paradigm revolves around the concept of “objects,” which can represent real-world entities. Here are the core principles of Java OOP:

  • Encapsulation: This principle involves bundling the data (attributes) and methods (functions) that operate as a single unit, known as a class. Encapsulation helps protect the internal state of an object by restricting access to its attributes through access modifiers (private, public, protected). This leads to improved data security and easier maintenance.
  • Inheritance: Inheritance allows one class to inherit the properties and behaviours of another class. This promotes code reusability and establishes a natural hierarchy among classes. For example, a Dog class can inherit from an Animal class, acquiring its attributes and methods while also adding specific features of Dog class.
  • Polymorphism: It can be defined as a combination of two words, poly means “multiple” and morphism means “forms”. In Java, this can be achieved through method overloading (same method name with different parameters) and method overriding (subclass providing a specific implementation of a method defined in its superclass).
  • Abstraction: Abstraction focuses on exposing only the necessary details of an object while hiding the complex implementation details. In Java, abstraction can be implemented using abstract classes and interfaces, allowing developers to define methods that must be implemented by subclasses without specifying how they should be implemented.

First Java Program

First Java Program
  • Java files have .java extension.
  • A Java file may contain more than one class but the file is named after primary class which has main method.
  • Keywords in main() method:
    • public – For JVM, it can be called from anywhere.
    • static – Method can be called without creating an instance of class.
    • void – Method does not return value to JVM.
    • String[] args – Useful when command line arguments are passed.

Code

Create a file and name it, HelloWorld.java and copy-paste following code:

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

Above Java program contains one class which has main() method which further contains only one print statement.

In simple words that print statement contains System keyword which is a built-in Java class defined in java.lang package. Next, out keyword represents an instance of PrintStream type and finally, println method (a public method of PrintStream class) which is used for printing message on new line.

First you need compile the java file (.java extension file). This file contains the source code and we pass this file to compiler by executing following command:

javac HelloWorld.java

Upon executing above command, it generates a machine-independent code (known as Bytecode) in the form of .class file for each class defined in the source code. This bytecode is secure, encrypted in binary only)

The class files generated by the compiler are independent of the machine or the OS (operating system), which complies WORA (write once and run anywhere) principle and allows them to be run on any system.

Next, in order to run it, execute following command:

java HelloWorld

Categorized in: