Skip to main content

Command Palette

Search for a command to run...

Introduction to Java

An Overview of Java’s Evolution, Core Components, and Basic Program Structure

Updated
4 min read
Introduction to Java

History of Java

1991 → Project started by James Gosling and team at Sun Microsystems
Initially named "Oak" after a tree near Gosling's office
Later renamed "Green" and finally "Java" after Java coffee

1995 → First public release of Java 1.0
Known for "Write Once, Run Anywhere" (WORA) — platform independence

1997 → Java Community Process (JCP) started for collaboration & standardization
JDK 1.1 released with major features like JavaBeans and JDBC
JVM standardized to ensure platform-independent execution

1998 → Release of Java 2 (J2SE 1.2), a major milestone
Introduced different editions: J2SE (Standard), J2EE (Enterprise), J2ME (Mobile)

2006 onwards → Naming changed to Java SE (Standard Edition), Java EE (Enterprise), Java ME (Mobile)

JDK, JVM, and JRE

  • JVM (Java Virtual Machine): The core runtime component that interprets compiled Java bytecode and translates it into machine-specific instructions. It manages memory, security, garbage collection, and provides a platform-independent execution environment.

  • JRE (Java Runtime Environment): Includes the JVM along with standard libraries and classes required to run Java applications. It does not include development tools like a compiler or debugger. JRE is for executing Java programs only.

  • JDK (Java Development Kit): This package includes the JRE plus development tools such as the Java compiler (javac), debugger, and other utilities required for writing, compiling, and debugging Java applications.

How a Java Program is Run

Running a Java program involves several steps:

  1. Writing the Source Code: Create a .java file with Java source code.

  2. Compilation: The javac compiler converts the source code into bytecode, a platform-independent intermediate format, stored in .class files.

  3. Loading: The JVM loads the bytecode into memory.

  4. Verification: JVM verifies the bytecode to ensure it is valid and secure.

  5. Execution: The JVM interprets or just-in-time compiles the bytecode into native machine code and runs the program.

Skeleton of a Java Program

import java.lang.*; // It will automatically be imported even if we don't import it
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • public: Access modifier, means the class/method is accessible from anywhere.

  • class: Defines a class named HelloWorld.

  • static: Means the method belongs to the class, not a specific instance. To use main method without creating an object of the class we use static here.

  • void: The method returns no value.

  • main: Entry point of any standalone Java application.

  • String[] args: An array of command-line arguments.

  • System.out.println: Prints text to the console.

    • System is a built-in class in the Java standard library (java.lang package).

    • out is a static variable inside the System class which points to an object of type PrintStream.

    • println is a method that belongs to the PrintStream class

Reading from Keyboard

The Scanner class in java.util package is used to read user inputs from various sources, including the keyboard.

Example

import java.util.Scanner;

public class InputExample {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); // Create Scanner object
        System.out.print("Enter your name: ");
        String name = input.nextLine(); // Reads a line of text
        System.out.println("Hello, " + name);

        input.close(); // Close the scanner
    }
}

Scanner input = new Scanner(System.in); creates a Scanner object to read input from the keyboard

Methods:

  • next(): Reads and returns the next single token (word) as a String.

  • nextLine(): Reads and returns an entire line of input as a String.

  • nextInt(): Reads the next token as an int value.

  • nextDouble(): Reads the next token as a double value.

  • nextFloat(): Reads the next token as a float value.

  • nextLong(): Reads the next token as a long value.

  • nextShort(): Reads the next token as a short value.

  • nextByte(): Reads the next token as a byte value.

  • nextBoolean(): Reads the next token as a boolean value.

  • hasNext(): Checks if there is another token available.

  • hasNextLine(): Checks if there is another line available.

  • Similarly hasNextInt(), hasNextFloat(), etc.

  • useRadix(int radix):Sets the default number base (radix) the scanner uses to interpret numeric input (e.g., decimal, hexadecimal). If radix = 2 then the input will be taken in Binary.

  • close(): Closes the Scanner object, freeing resources.