← Back to Midterms

Introduction to Java

Explanation & Learnings

Reflecting on the foundations of Java, the concept of "Write Once, Run Anywhere" (WORA) is honestly impressive, as it addresses the challenges of cross-platform compatibility. By using a Java compiler to transform source code into bytecode rather than direct machine code, Java creates a universal language that any system with a Java Virtual Machine (JVM) can understand. This architecture demonstrates how a clever intermediate step can bridge the gap between different operating systems.

1. What is Java programming language? Discuss its key features and advantages.

Java is a high-level, object-oriented programming language developed by Sun Microsystems on 1995 (now Oracle). The first stable version was released on 1996.

Key Features:

  • Object-oriented - Java uses objects & classes to organize code and supports: encapsulation, inheritance, polymorphism, and abstraction.
  • Platform Independent - Java programs can run on any system without modification. (Write Once, Run Anywhere).
  • Simple & Secure - Easy to learn compared to C++. Also, provides built-in security features such as no direct memory access and bytecode verification.
  • Robust & Portable - Java has strong memory management, exception handling and type checking. It also can run on any OS such as Windows, MacOS and Linux.

2. Explain the concept of platform independence in Java. How does Java achieve it?

Platform independence means that the program can be ran on any operating system without changing the code. How Java achieves platform independence depends on the java source code (.java) and file compiler bytecode (.class). During compilation, the Java compiler (Javac) compiles the file into an immediate representation called bytecode. Next, the JVM which is platform-specific and installed separately in each OS, reads and executes bytecode. So, JVM is platform-dependent and the bytecode is platform-independent. This is the core mechanism behind WORA.

3. Discuss the main components of a Java program and their roles.

Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}
  • Class = public class Hello → a class is a blueprint for objects, every Java program must have at least one class.
  • main() method = public static void main(String[] args) → entry point of the program. Java execution starts here.
  • Access Modifier = public → defines visibility.
  • static keyword → the method belongs to the class, not an object.
  • void keyword → the method does not return any value.
  • method body → contains instructions to execute.
  • statement → displays output.

4. What are the differences between JDK, JRE and JVM?

  • JVM (Java Virtual Machine) - JVM runs Java programs. Functions: executes bytecode, converts bytecode to machine code.
  • JRE (Java Runtime Environment) - JRE provides the environment to run Java programs. Contains: JVM, libraries, files. JRE = JVM + libraries.
  • JDK (Java Development Kit) - JDK is used to develop Java programs. Contains: JRE, compiler (Javac), tools (debugger). JDK = JRE + dev tools.

5. Explain the difference between interpreted and compiled languages. Is Java interpreted or compiled?

Java is both compiled and interpreted. Compiled language means that the source code is converted into machine code before execution. It is platform-dependent.

Source code → compiler → machine code → Run

Interpreted Language means that the code is executed line-by-line by an interpreter. It is platform-independent.

Source code → Interpreter → run directly

Java is considered compiled and interpreted because of the nature of the language and its compilation processes.

  • Compiled: Java source code → compiled into bytecode
  • Interpreted: JVM interprets bytecode → machine code