02 Java - Components, Editions and Insights

Java in Brief

  • Platform Independent Language

    • Portability (WORA) - Write Once, Run Anywhere

    • Write Java code in your mobile then also you can run the same code in mobile or desktop

  • Popular OOPs language

3 Main Components of Java

I. JVM (Java Virtual Machine)

  • It is an abstract machine means it is a software. (Not a physical machine)

  • JVM is platform dependent. For example if we have window os then we require windows JVM and similarly for mac os we require it's particular compatible JVM.

  • JVM has JIT (Just in time compiler) compiler.

JVM input is byte code and output is machine code.

II. JRE (Java Runtime Environment)

  • JVM + Class Libraries

  • Class libraries supply all the inbuilt libraries which the byte code is referring. (Example java.math, java.util)

III. JDK (Java Development Kit)

  • JDK supplies = JVM + (the following 3)

    • Compiler (javac)

    • Debugger

    • Programming Language (.java)

Platform

  • Dependent - JDK, JRE, JVM

  • Independent - Byte Code

Editions of Java

I. JSE - Java Standard Edition

JSE nothing but the core java. It has all the classes, multithreading..etc.

II. JEE - Java Enterprise Edition

JEE = JSE + APIs

APIs include (Transactional API + Roll back+ Servlets + JSP +...+ Perisistance API +..)

JEE is used for large scale applications like e-commerce.

JEE is also Jakarta Enterprise Edition

III. JME - Java Mobile Edition

JME provides API's for mobile applications.

Java Program Insights

  • File Name is same as the Class Name

  • One File can have one public class

  • Class Structure

  • main() - Main Method: It is the starting point of the program. JVM calls the main() method to start running the program.

  • Comments:

    1. Single Line Comments (//)

    2. Multi Line Comments (/* ... */)

    3. Javadoc Comments (/** ... */) - This is for documentation purpose. This generates API documentation automatically.

public class MyClass {
    /** 3 Doc
     * It describes the functionality of the method.
     * @param x This is the first parameter of the method.
     * @param y This is the second parameter of the method.
     * @return The sum of x and y.
     */
    public int add(int x, int y) {

        /* 2 Multi line
            testing other addition logics
            return (x+y);
        */
        // 1. Single line
        return x + y;
    }
}

Interview Question

Why in Java one file can have only 1 Public class?

  1. The main method should be inside a public class: The main method serves as the entry point for the Java Virtual Machine (JVM) to execute a Java program. To access the main method, it must be within a class that is declared as public, and the main method itself should also be declared as public.

  2. The name of the public class should match the file name: When a Java program is compiled, each public class is typically stored in its own file, with the file name matching the class name. This convention allows the JVM to locate the class definition efficiently. If a file contains multiple public classes, the JVM might have difficulty determining which class contains the main method—the entry point of the program. Therefore, by matching the public class name with the file name, the JVM can directly access the appropriate class, which aids in program execution without the need for extensive searching.

Consequently, a Java source file should ideally contain only one public class declaration, ensuring clarity and efficiency in program execution.