This lecture describes how we set up the environment for executing Java Application and also describes the tools that are required. This lecture also contains a simple java program and the description.
2. Contents
Java Environment Set up
Simple Java Program
Internal details of Java Programs
JDK, JRE & JVM
3. Environment Set up
To set path of java is required when you save source file (i.e., .java file) any where in your system except bin
directories.
To set environment for java programming you must follow the steps given below:
Download JDK according to the version of Operating System.
Run the .exe file and install it.
You will need to set path to point to correct installation directories.
To set permanent path for windows:
Suppose you have installed jdk in C:Program Filesjavajdk directories:
Right Click on Computer and then select Properties
Click on Advanced tab and then click on Environment Variable button
Click on new button under user variable/system variable and write variable name as path & variable
value as: ‘C:Program Filesjavajdkbin'
4. Environment Set up
To set temporary path for windows:
Press Window + R button on keyboard and type cmd in run prompt and click ok button (i.e. open
command prompt)
Copy the path of jdk/bin directories
Type set path = copied path
For Example:
Set path = ‘C:Program Filesjavajdkbin’
To set path for Linux:
To set the path of java in Linus is same as windows but in the case of Linux we use the export
tool rather than set. Path in Linux is look like as
export PATH = $PATH:/home/jdk1.6.01/bin/
5. Simple Java Program
To write a simple Java Program, we must know some basic points like class, object, etc. These are described
briefly as follows:
Object is a real-world entity and it has state and behavior. An object can also be defined as instance of the
class.
Class can be called as group of similar type of objects. Class is like a template that describe the
state/behavior of objects.
Methods: We write logics, data is manipulated and all actions are executed in methods. There can be many
methods in a class.
Instance Variable: Every object has its unique set of instance variable. Instance variable is declared out side
the methods i.e. at class level.
6. Simple Java Program
First Java Program:
public class FirstProgram {
public static void main (String args []) {
System.out.println(“First Java Program”)
}
}
Save this file as FirstProgram.java
To compile: javac FirstProgram.java
To Execute: java FirstProgram
Output First Java Program
7. Simple Java Program
Important Points of Java Program:
Java is case sensitive i.e. identifier Hello and hello would have different meaning
First letter of class name of any java program should be written in upper case. If any class name has multiple
words then every inner word’s first letter should be in upper case. (It’s only a convention rule.)
Ex. class FirstProgram
The methods of java program should start with lower case letter. If methods name has several words then
except first word’s letter all inner word’s first letter should be in upper case.
Ex. public void getData()
Program file name and class name both should be match.
Program processing starts from public static void main(String args[]).
8. Simple Java Program
Parameters used in first Java Program:
class is a keyword used to declare a class.
public is an access modifier used for visibility.
static is a keyword used to make a method as static and the advantage of static method is that there is no need
to create an object to invoke the static method.
void is the return type and it means it does not return any value.
main is the starting point of a program and it is name of the method.
String args[] is used for command line argument. Here String is predefined class and args is array name.
To print any statement, we use System.out.println(), in which System is a class, out is the reference
variable (i.e. initialized by reference data type) of PrintStream class and println() is the method of
PrintStream class.
9. Internal details of java programs
At compile time by java compiler it does not interact with Operating System and converts the java code into
byte code or .class file.
10. Internal details of java programs
Following steps are performed at run time:
11. Internal details of java programs
Class loader – Subsystem of JVM that is used to load the class file.
Bytecode Verifier – To check the code for illegal that can violate access right to objects.
Interpreter – Used to read the bytecode stream and then executes the instructions.
12. JDK, JRE and JVM
JVM (Java Virtual Machine)
A virtual machine that provides the run-time environment to execute the java application.
JVM is platform dependent because the OS configuration are different from each other.
There are following task performed by JVM:
o Loads the code
o Verifies the code
o Executes the code
o Provides runtime environment
14. JDK, JRE and JVM
JRE (Java Run-time Environment)
JRE provides set of software tools to develop java applications. JRE physically exist and contains set of
libraries & other files that JVM uses at runtime.
15. JDK, JRE and JVM
JDK (Java Development Kit)
JDK contains all the tools that are required to develop java applications and applets. JDK physically exist.