Inetsolv Core Java
Inetsolv Core Java
You should have already installed Java Development Kit (JDK). Otherwise, Read "How to
Install JDK and Get Started with Java Programming".
Let us begin by writing our first Java program that prints a message "Hello, world!" to the
display console, as follows:
Hello, world!
Step 1: Write the Source Code: Enter the following source codes using a programming text
editor (such as TextPad or NotePad++ for Windows; jEdit or gedit for Mac OS X; gedit for
Ubuntu); or an Interactive Development Environment (IDE) (such as Eclipse or NetBeans).
Do not enter the line numbers (on the left panel), which were added to aid in the
explanation. Save the source file as "Hello.java". A Java source file should be saved with a
file extension of ".java". The filename shall be the same as the classname - in this case
"Hello".
1 /*
3 */
7 }
8 }
Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable
bytecode "Hello.class" using JDK Compiler "javac". Start a CMD Shell (Windows) or Terminal
(UNIX/Linux/Mac OS X) and issue this command:
There is no need to explicitly compile the source code under IDEs (such as Eclipse or
NetBeans), as they perform incremental compilation implicitly.
Step 3: Run the Program: Run the program using Java Runtime "java", by issuing this
command:
Hello, world!
Take note that the Run command is "java Hello" without the ".class" extension.
On IDEs (such as Eclipse or NetBeans), right-click on the source file ⇒ Run As... ⇒ Java
Application.
/* ...... */
// ... until the end of the line
These are called comments. Comments are NOT executable and are ignored by the
compiler. But they provide useful explanation and documentation to your readers (and to
yourself three days later). There are two kinds of comments:
1. Multi-line Comment: begins with /* and ends with */, and may span more than one
lines (as in Lines 1-3).
2. End-of-line Comment: begins with // and lasts until the end of the current line (as in
Lines 4 and 6).
In Java, the name of the source file must be the same as the name of the public class with a
mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".
Comments: A multi-line comment begins with /* and ends with */. An end-of-line comment
begins with // and lasts till the end of the line. Comments are NOT executable statements
and are ignored by the compiler. But they provide useful explanation and
documentation. Use comments liberally.
Whitespaces: Blank, tab, and newline are collectively called whitespace. Extra whitespaces
are ignored, i.e., only one whitespace is needed to separate the tokens. But they could help
you and your readers better understand your program. Use extra whitespaces liberally.
Case Sensitivity: Java is case sensitive - a ROSE is NOT a Rose, and is NOT a rose.
The filename is also case-sensitive.
You can use the following template to write your Java programs. Choose a meaningful
"Classname" that reflects the purpose of your program, and write your programming
statements inside the body of themain() method. Don't worry about the other terms and
keywords now. I will explain them in due course.
4 }
5 }
System.out.print(aString) prints aString but places the cursor after the printed string.
Try the following program and explain the output produced:
4 System.out.println("Hello, world!"); // Advance the cursor to the beginning of next line after printing
7 System.out.println("Hello,");
9 System.out.print("world!");
10 System.out.println("Hello, world!");
11 }
12 }
Hello, world!
Hello, world!Hello,
world!Hello, world!
JAVA_HOME
C:\Program Files\Java\jdk1.8.0_20
JRE_HOME
C:\Program Files\Java\jre1.8.0_20
Path
JAVA_HOME\bin; JRE_HOME\bin;
JVM Architecture
JVM Architecture
JVM has various sub components internally. You can see all of them from the above diagram.
1. Class loader sub system: JVM's class loader sub system performs 3 tasks
a. It loads .class file into memory.
b. It verifies byte code instructions.
c. It allots memory required for the program.
2. Run time data area: This is the memory resource used by JVM and it is divided into 5 parts
a. Method area: Method area stores class code and method code.
b. Heap: Objects are created on heap.
c. Java stacks: Java stacks are the places where the Java methods are executed. A Java stack
contains frames. On each frame, a separate method is executed.
d. Program counter registers: The program counter registers store memory address of the
instruction to be executed by the micro processor.
e. Native method stacks: The native method stacks are places where native methods (for
example, C language programs) are executed. Native method is a function, which is written in another
language other than Java.
3. Native method interface: Native method interface is a program that connects native methods
libraries (C header files) with JVM for executing native methods.
5. Execution engine: Execution engine contains interpreter and JIT compiler, which covert byte code
into machine code. JVM uses optimization technique to decide which part to be interpreted and which
part to be used with JIT compiler. The HotSpot represent the block of code executed by JIT compiler.