Introduction to java
The aim of Java is to write programs that runs on variety of computer systems and computer controlled devices. This is often called as "Write once run anywhere". This Session explains Java's typical development environment.2
Your First Java Program
In this session we will write a simple Java program, understand memory concepts, use arithmetic operations, use input and output statements, etc.1
How to Compile Java Program
Java program shall be created, compiled, and executed in two different ways. One is the traditional way of using text editors like Notepad, Notepad++, TextEdit, EditPlus, etc., and compiling on the terminal window.
I will be using NetBeans IDE throughout this course as it is owned by oracle – the same company which owns Java.
Benefits of Using IDEs for Compiling Java Program
- Most IDE’s are open source software – means they are free.
- Organization: It is very easy to organize files using IDE.
- Efficiency: You can code, compile and execute programs in IDEs much faster.
- Debugger: Debugger instantly reports your program errors even before compiling. This helps the programmer to save much time.
- Intelligence: IDEs assist in suggesting keywords like functions, variables, class hierarchy., then and there to the programmers. For example, whenever you call a function, its arguments are automatically suggested for you.
- Collaboration: IDE’s helps colleagues to work together on large projects.
Java Development Process
The Java development (create, compile and execute) process has 5 phases.
Phase 1: Creating a Program
This phase has the following process:
- Java program is typed in a text editor and saved on the computer hard drive. The file must be saved with a .java extension. This is an indication to the compiler that this file contains a java program.
- This program typed on a text editor is known as source code.
- For Example, let us type a java code in a file and name it HelloWorld.java
- There are two types of text editors: Conventional text editors such as Notepad, TextEdit, etc., and modern IDE’s such as NetBeans, Eclipse, etc.
Note: We use NetBeans IDE throughout this course.
Phase 2: Compiling a Java Program
In this phase, we compile a java program into ByteCodes
- For this, we need to call the java compiler called javac. (javac is the name of the java compiler).
- Now the question is how to call this java compiler?
- If you are running your Java program on Command Prompt such as Windows Command Prompt or Terminal in Mac or Shell in Linux, you need to type the following command to compile.
javac filename.java [Example: javac HelloWorld.java]
- If you are using any IDE’s like Eclipse, NetBeans, IntelliJ Idea, etc., you can just click on the Build/Make/Run buttons to compile the program easily. This invokes the javac command for you.
- If your program has no errors, it will be compiled successfully. This creates a .class file called filename.class. [in our example, HelloWorld.class]
- This .class is the compiled version of your program, aka ByteCode.
Phase 3: Loading a Program in to Memory
A Note on JVM
Java Virtual Machine
- The ByteCode (.class file) is architecture-neutral and can be run on any machine that has the JVM – Java Virtual Machine.
- A JVM is a program that gets installed with the SDK (Java Software Development Kit) in your machine. This JVM interprets the Java bytecode.
- This is the primary advantage of Java. Java ByteCode can run on a variety of hardware platforms and operating systems.
- JVM translates the bytecode line by line into machine-understandable instructions.
- Here memory refers to RAM (Random Access Memory).
- JVM is a very complex software that has many parts. One such part is the class Loader. The JVM’s class loader takes the .class (bytecode) files containing the program’s bytecodes and transfers them to primary memory (RAM).
- Note: The .class files can be loaded from your hard disk or from any network.
Phase 4 - Bytecode Verification
- Another part of the JVM software is the bytecode verifier, which examines the bytecodes to ensure that they are valid and do not violate java’s security restrictions.
- Sometimes the bytecode arriving from the network may have viruses that may harm your computer.
Phase 5 - Execution
- The word Execution means that your program is actually running on your machine (computer).
- The JVM executes the bytecodes on your machine, thus performing the actions specified by the program.
The java compilation process actually happens in phases.
- The source code is translated into bytecode by the java compiler.
- The bytecode is translated into machine-understandable code by the JVM across platforms.
- What is an Interpreter?
- An Interpreter executes one line of code at a time and translates it into machine code.
- Early versions of JVM simply interpret the bytecodes which are very slow.
- What is a compiler?
- A compiler executes the entire program into machine-understandable code which is much faster.
- Just in time Compiler (JIT)
- Java uses a combination of the interpreter and a just-in-time compiler.
- JVM scans the entire bytecode and analyses the frequently used bytecodes.
- The JIT-Compiler translates these frequently used bytecodes into machine codes.
- When these bytecodes appear again in the program, the faster machine code executes.