Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Soundarya Institute of Management and

Science

Chapter 3
Overview of Java
Language

Sheela D V - Department of Computer Science 1


Types of Java Program
Java Program

 Application Type
Stand alone Programs Programs

• Example of SAP: sum of two numbers


• Example of ATP: Applet Programming

Sheela D V - Department of Computer Science 2


Phases of java program development

• EDIT – Write the program and saves it as a ‘.java’ file

• COMPILE – The compiler here takes the ‘.java’ file, compiles the program
and then the byte code is generated and saved as ‘.class’ file.

• LOAD – ‘Class Loader’ is to load the byte code in the JVM.

• VERIFY – The JVM’s subpart called ‘Byte Code verifier’ checks the byte
code and confirms that all byte code are valid and do not violate Java’s
security restrictions.

• EXECUTE – The execution engine interprets the code line by line using the
Just In Time (JIT) compiler. The JIT compiler does the execution pretty fast.

Sheela D V - Department of Computer Science 3


First Java Program

• Step 1: Write the below program in the notepad

class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

• Step 2: Save the file name as Simple.java


• Step 3: Compile the Program with the command ‘javac’.
• Step 4: The byte code Simple.class is generated.
• Step 5: Execute the program using java.exe and the output is displayed
in console.

Sheela D V - Department of Computer Science 4


Understanding the Program

• class keyword is used to declare a class in java.

• public keyword is an access modifier which represents visibility, it


means it is visible to all.

• static is a keyword, if we declare any method as static, it is known


as static method. The core advantage of static method is that there
is no need to create object to invoke the static method. The main
method is executed by the JVM, so it doesn't require to create
object to invoke the main method. So it saves memory.

Sheela D V - Department of Computer Science 5


Contd…..

• void is the return type of the method, it means it doesn't return any value.

• main represents startup of the program.

• String[] args is used for command line argument.

• System.out.println() is used print statement.

Sheela D V - Department of Computer Science 6


Second Java Program

class AddNumbers {
public static void main(String args[]) {
int x, y, z;
x = 10;
y = 10;
z = x + y;
System.out.println("Sum of integers = "+z);
}
}

Sheela D V - Department of Computer Science 7


Program with multiple classes

class Computer {
Computer() {
System.out.println("Constructor of Computer class."); }
void computer_method()
{
System.out.println("Power gone! Shut down your PC soon...");
}
public static void main(String[] args) {
Computer.computer_method();
Laptop.laptop_method();
}
}
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
}
void laptop_method() {
System.out.println("99% Battery available.");
Sheela D V - Department of Computer Science 8
}
Java Program Structure

Sheela D V - Department of Computer Science 9

You might also like