Skip to content

About Java

INFO

Created by James Gosling in 1991 Was initially named Oak , later renamed to Java

Compilation process

Java Compiler (javac)

Java compiler compiles source code into bytecode.

Source Code is the Java program written in a file with a '.java' extension.

Java bytecode files end with a '.class' extension. This file makes Java Platform Independent. It is the machine language for Java Virtual Machine.

Java Virtual Machine (JVM)

It is a part of Java Runtime Environment (JRE) .It is a virtual machine which reads bytecode and interprets machine code depending on underlying operating system and hardware.

Just In Time (JIT) Compiler

It is a part of JVM. Converts Bytecode to native executable code in real time, on a piece by piece basis.(Machine code). This machine code is machine dependent.The use of JIT is optional as it serves to increase the performance of program execution.

Some examples of native machine code file extensions :

.exe for Windows

.osx for MacOs (Not in syllabus)

.out for linux (Not in syllabus)

What is Bytecode file? Why it is called a Bytecode file?

Bytecode is the intermediate object code generated by java compiler (javac).Bytecode is essentially the object code for the Java Virtual Machine (JVM). The JVM is a software environment that interprets and executes this bytecode.

It is called bytecode because the instructions in this intermediate code are typically one byte in size (an opcode, or operation code)

Source code
java
class Main {
    public static void main(String args[]) {
        System.out.println("Hello World");
    }
}
Bytecode
java
class Main {
  Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #13                 // String Hello World
       5: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}
Machine code
asm
...
0x00007f2a480a11a0: mov     %r9d,%r10d
0x00007f2a480a11a3: mov     0x10(%r15),%rdi
0x00007f2a480a11a7: mov     %r10d,0x14(%r15)
0x00007f2a480a11ac: callq   0x00007f2a47e9e590  ; {inline, _ZN3oop19check_and_handle_async_exceptionsEv}
0x00007f2a480a11b1: mov     0x78(%r13),%rax
0x00007f2a480a11b5: mov     %rax,(%rsp)
0x00007f2a480a11b9: mov     $0x2a,%esi        ; {oop(a 'java/lang/String')}
0x00007f2a480a11be: callq   0x00007f2a47f0c1a0  ; {runtime_call(void (AbstractThread::*)(oop))}
0x00007f2a480a11c3: mov     0x88(%r13),%rax
0x00007f2a480a11ca: mov     %rax,0x20(%rsp)
0x00007f2a480a11cf: movl    $0xa,0x28(%rsp)
0x00007f2a480a11d7: mov     %r12,0x30(%rsp)
0x00007f2a480a11dc: mov     %rbp,0x38(%rsp)
0x00007f2a480a11e1: mov     %r14,0x40(%rsp)
0x00007f2a480a11e6: mov     %r15,0x48(%rsp)
0x00007f2a480a11eb: callq   0x00007f2a480694a0  ; {call java/io/PrintStream.println(String) }
0x00007f2a480a11f0: nop
0x00007f2a480a11f1: pop     %r15
0x00007f2a480a11f2: pop     %r14
0x00007f2a480a11f3: pop     %rbp
0x00007f2a480a11f4: pop     %r12
0x00007f2a480a11f5: add     $0x58,%rsp
0x00007f2a480a11f9: retq
...
Machine code in binary form

Are you serious? x86 Assembly wasn't enough for you? Go get a life.

Difference between compiler and interpreter

CompilerInterpreter
Scans the entire program and translates it as a whole into machine code.Translates program one statement at a time.
The compiler shows all errors and warnings at once.Interpreter only shows one error at a time.
It generates intermediate object code.It does not generate any intermediate object code.
The compilation is done before execution.Compilation and execution takes place simultaneously.

NOTE

Here translation means conversion of source code into machine code

Characteristics of Java

Write Once Run Anywhere (WORA)

Java programs written once can be run on different platforms without making any changes to the program.

Light Weight Code

No huge coding is required.

Security

Java offers many enhanced security features.

Object-Oriented Language

Java is object-oriented, hence very near to real world.

Platform Independent

Java is essentially platform independent. Change of platform does not affect original Java program/application.

Types of Java Programs

Internet Applets

Internet Applets are small programs that are embedded in web pages and are run on the viewer's machine in a secured manner (which means limited access to system resources i.e., the hard disk) by Java capable browsers.

Applets are designed to be delivered to Internet Web browsers and that is why an applet has a built-in graphical window. But Java applets have some security restrictions (e.g., it cannot write to a local file).

Standalone Application

It is generally a software application that does not require low level operating system or hardware access. This includes most off the usual desktop applications such as word processors or spreadsheets. Standalone Java applications could be distributed and installed on any Java capable machine. Every stand alone application of Java begins executing with a main method.

Packages in Java

java.applet : Java Applet Package

java.awt : Java Abstract Window Toolkit Package

java.io : Java Input/Output Package

java.lang : Java Language package

java.net : Java Networking Package

java.util : Java Utility Package


Token

Smallest individual unit in a program is called a token. Keywords, identifiers, literals, operators and punctuators are tokens in a Java program.