• working principal of java

    working principal of java 

    Java, being a platform independent programming language, doesn’t work on one-stepcompilation.
    Instead, it involves a two-step execution, first through an OS independent
    compiler; and second, in a virtual machine (JVM) which is custom-built for every operating
    system. The two principle stages are explained below


     i. Compilation

    First, the source ‘.java’ file is passed through the compiler, which then encodes the source code into a machine independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate ‘.class’ file. While converting the source code into the bytecode, the compiler follows the following steps:

    • Parse

    Reads a set of *.java source files and maps the resulting token sequence into AST (Abstract Syntax Tree)-Nodes.

    • Enter

    Enters symbols for the definitions into the symbol table.

    • Process annotations

    If Requested, processes annotations found in the specifed compilation units.

    • Attribute

    Attributes the Syntax trees. This step includes name resolution, type checking and constant folding.

    •  Flow

    Performs dataflow analysis on the trees from the previous step. This includes checks for assignments and reachability.

    •  Desugar

    Rewrites the AST and translates away some syntactic sugar.

    • Generate:

     Generates ‘.Class’ files.

    ii. Execution

    The class files generated by the compiler are independent of the machine or the OS, which allows them to be run on any system. To run, the main class file (the class that contains the method main) is passed to the JVM, and then goes through three main stages before the final machine code is executed. 

    These stages are:

    Class Loader

    The main class is loaded into the memory by passing its ‘.class’ file to the JVM, through invoking the latter. All the other classes referenced in the program are loaded through the class loader.

    Bytecode Verifier

    After the bytecode of a class is loaded by the class loader, it has to be inspected by the bytecode verifier, whose job is to check that the instructions don’t perform damaging actions. The following are some of the checks carried out:

    • Variables are initialized before they are used.
    • Method calls match the types of object references.
    • Rules for accessing private data and methods are not violated.
    • Local variable accesses fall within the runtime stack.
    • The run time stack does not overflow.

    If any of the above checks fails, the verifier doesn’t allow the class to be loaded.

    Just-In-Time Compiler

    This is the final stage encountered by the java program, and its job is to convert the loaded bytecode into machine code. When using a JIT compiler, the hardware can execute the native code, as opposed to having the JVM interpret the same sequence of bytecode repeatedly and incurring the penalty of a relatively lengthy translation process. This can lead to performance gains in the execution speed, unless methods are executed less frequently.

    Due to the two-step execution process described above, a java program is independent of the target operating system. However, because of the same, the execution time is way more than a similar program written in a compiled platform-dependent program.

    Java LayoutManagers

    The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers:

    1. java.awt.BorderLayout
    2. java.awt.FlowLayout
    3. java.awt.GridLayout
    4. java.awt.CardLayout
    5. java.awt.GridBagLayout


  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!