Advance Computer Programming
By
Hasnat Ali
Contents
Difference between JRE, JDK, JVM, and JIT
Java Execution Flow
Compiling and Execution of Java Program through
Command Prompt
path and CLASSPATH
Anatomy of Java Program
Summary
Difference between JDK, JRE, JVM, and JIT
JDK (Java Development Kit)
JDK contains tools required to develop Java
Programs. Therefore, JDK mainly targeted for java
development
Used for creation and compiling of java programs
Tools included compiler (javac.exe), java
application launcher (java.exe), appletviewer etc.
JVM (Java Virtual Machine)
'.class' file consists of Java byte code; understandable by
JVM.
JVM interprets the byte code into the machine code
depending upon the underlying operating system and
hardware combination.
It is responsible for all the things like garbage collection,
array bounds checking, etc.
The JVM is called "virtual" because it provides a machine
interface that does not depend on the underlying
operating system and machine hardware architecture.
JIT (Just In-Time Compiler)
JIT is the part of the Java Virtual Machine (JVM) that is
used to speed up the execution time.
In JIT (also known as dynamic translation), compilation
done during execution of a program – at run time –
rather than prior to execution. Most often this refers to
translation to machine code, which is then executed
directly, but can also refer to translation to another
format.
Here the term “compiler” refers to a translator from the
instruction set of a Java virtual machine (JVM) to the
instruction set of a specific CPU.
JRE (Java Run Time Environment)
Contains JVM, class libraries, and other supporting
files.
It does not contain any development tool i.e.
compiler or debugger
JVM becomes an instance of JRE at runtime of a java
program. It is widely known as a runtime interpreter.
JAVA Program
and
Execution Flow
A Simple Java Program
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
A Simple Java Program
import java.lang.*;
//This program prints Welcome to Java!
public class Welcome extends Object{
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Enter main method
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Execute statement
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
print a message to the
console
Compiling and Running Java from the Command Window
Set path to JDK bin directory
set path=c:\Program Files\java\jdk1.8.0\bin
Set classpath to include the current directory
set classpath=.
Compile
javac Welcome.java
Run
java Welcome
path and classpath
Path is an environment variable which is used by the
operating system to find the executable.
Classpath is an environment variable which is used by
the Java compiler to find the path of classes i.e. in J2EE
we give the path of jar files.
In short,
PATH = only for executable
CLASSPATH = for directories containing .class files,
JAR files & ZIP files.
path and classpath
PATH is nothing but setting up an environment for operating
system. Operating System will look in this PATH for executable.
Classpath is nothing but setting up the environment for Java. Java
will use to find compiled classes (i.e. .class files).
For example, assume that Java is installed in C:\jdk1.7.0\ and your
code is in C:\Sample\example.java, then your PATH and
CLASSPATH should be set to:
PATH = C:\jdk1.7.0\bin
CLASSPATH = D:\Sample
Anatomy of a Java Program
Import package
Comments
Reserved words
Modifiers
Statements
Command-Line Arguments
Classes (constructor, data fields, Methods)
The main method
Importing package
No need to write explicitly
import java.lang.*;
But there is need to write it for importing other
packages
Comments
Three types of comments in Java.
Line comment: A line comment is preceded by two slashes
(//) in a line.
Paragraph comment: A paragraph comment is enclosed
between /* and */ in one or multiple lines.
javadoc comment: javadoc comments begin with /** and
end with */. They are used for documenting classes, data, and
methods. They can be extracted into an HTML file using JDK's
javadoc command.
Documentation Comments
javadoc comment: javadoc comments begin with /** and
end with */. They are used for documenting classes, data, and
methods. They can be extracted into an HTML file using JDK's
javadoc command.
For javadoc (in default mode) to extract a comment, the
comment must satisfy two conditions:
The comment must immediately precedea public class
definition, a public method definition, or other public
item.
The comment must be a block comment (that is, the
/*and */style of comment),
Documentation Comments - Example
/*
* My First Program in Java.
* Just to execute a line of code.
*/
import java.lang.*;
@tag: The special /**
information about * This will print Welcome message
* @author Liang
some parameters */
is preceded by the public class Test {
@symbol and are /**
* This class has main method
called@tags. */
public static void main(String[] args) {
// Line to print
System.out.println("Welcome Java");
}
}
Documentation Comments – Running javadoc
Run javadoc on a single class file will produce documentation
for the class Welcome:
javadoc Test.java
You can run javadoc on all classes in a directory with
javadoc *.java
Documentation Comments – Running javadoc
Documentation Comments – Running javadoc
Running javadoc - Options
Run javadoc with various options i.e.
javadoc –d Documentation_Directory Package_Name
Reserved Words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used
for other purposes in the program.
For example, when the compiler sees the word class,
it understands that the word after class is the name
for the class. Other example reserved words are
public, static, void etc.
Reserved Words
Modifiers
Java uses certain reserved words called modifiers that
specify the properties of the data, methods, and
classes and how they can be used.
Examples of modifiers are public and static. Other
modifiers are private, final, abstract, and protected.
A public datum, method, or class can be accessed by
other programs. A private datum or method cannot
be accessed outside the class of its declaration.
Statements
A statement represents an action or a sequence of
actions e.g. following statement
System.out.println("Welcome to Java!")
in the program is a statement to display the greeting
"Welcome to Java!“
Every statement in Java ends with a semicolon (;).
Command-Line Arguments
class Test{
public static void main(String args[]){
int x;
for(int i=0; i<args.length; i++){
x = Integer.parseInt(args[i]);
System.out.println(x);
}
}
}
For Each Loop
public class Test{
public static void main(String args[]) {
int arr[] = {11, 33, 88, 90};
for(int i : arr)
System.out.println(i);
}
}
public class Test{
public static void main(String args[]) {
String arr[] = {"aa", "bb", "cc"};
for(String i : arr)
System.out.println(i);
}
}
Classes
The class is the essential Java construct.
A class is a template for objects.
To program in Java, you must understand classes
and be able to write and use them.
System.out.println()
What is System.out.println?
It is a method: a collection of statements that performs a
sequence of operations to display a message on the
console. It can be used even without fully understanding
the details of how it works.
It is used by invoking a statement with a string argument.
The string argument is enclosed within parentheses. In
this case, the argument is "Welcome to Java!“.
main Method
The main method provides the control of program
flow. The Java interpreter executes the application by
invoking the main method.
The main method looks like this:
public static void main(String[] args) {
// Code Statements;
}
Summary
Difference between JDK, JRE, JIT, and JVM
Java Execution Flow
path and classpath setting
Compiling and Execution of Java Program with
Command Prompt
Anatomy of Java Program