Java Introduction

1. History and Features of Java

Java is a programming language originally developed by James Gosling and his team at Sun Microsystems in 1995. It was designed to be a versatile, platform-independent, and secure language, suitable for developing various applications. The initial name for Java was "Oak," later renamed Java, inspired by the coffee that fueled the development team. After Oracle Corporation acquired Sun Microsystems in 2010, it became the current maintainer of Java.

Key Features of Java:

  • Platform Independence: Java's platform independence stems from its ability to compile programs into bytecode, allowing them to run seamlessly on any device equipped with a Java Virtual Machine (JVM).
  • Object-Oriented: Java is designed around objects, supporting principles like inheritance, polymorphism, and encapsulation.
  • Robust: With features like garbage collection and exception handling, Java ensures stable and error-free execution.
  • Secure: Java restricts unauthorized access by utilizing bytecode verification and runtime security checks.
  • Multithreaded: Java supports concurrent programming, allowing multiple tasks to run simultaneously.
  • High Performance: While not as fast as low-level languages, Java's Just-In-Time (JIT) compiler boosts its runtime performance.

Example of Platform Independence:

Write the code once and execute it on any operating system (Windows, Linux, macOS) without modifications:

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

2. Setting Up the Development Environment

Before coding in Java, you must prepare a development environment by following these steps:

Step 1: Install the Java Development Kit (JDK)

  • Download the latest version of the JDK from Oracle's official website or an open-source alternative like OpenJDK. The JDK includes the Java Runtime Environment (JRE), compilers, and tools for development.

Step 2: Configure the Environment Variables

  • Once the JDK is installed, configure the JAVA_HOME environment variable to point to the installation directory.
  • Additionally, include the JDK's bin directory in the PATH variable. This enables you to execute Java commands (such as javac and java) directly from the terminal.

Step 3: Install an Integrated Development Environment (IDE)

  • Use an IDE like IntelliJ IDEA, Eclipse, or NetBeans for writing, debugging, and running Java programs. Alternatively, a simple text editor like Visual Studio Code or Notepad++ can be used with manual compilation.

Step 4: Verify Installation

  • Run the following commands in a terminal or command prompt to confirm the installation:
java -version

3. Compiling and Running Java Programs

Writing a Java Program

Create a .java file containing your Java code. For example, save the following code as Example.java:

public class Example {
    public static void main(String[] args) {
        System.out.println("Java is running!");
    }
}

Compiling the Java Program

To convert the source code into bytecode, use the javac compiler. This creates a .class file, which can be executed by the JVM:

javac Example.java

If there are no errors, a Example.class file is generated in the same directory.

Running the Java Program

Execute the compiled program with the java command:

java Example

The output will be:

Java is running!

Explanation:

  • Compilation (javac): Translates human-readable code into bytecode.
  • Execution (java): The JVM interprets the bytecode and runs the program.
Next