You are on page 1of 54

Chapter 1

Introduction to Java
Java Virtual Machine (JVM)

Java Development Kits (JDK)

Java Runtime Environment (JRE)

Compile and run first Java Program

Object Oriented Programming


• Class Loader: The class loader reads the .class file and save
the byte code in the method area.
• Method Area: There is only one method area in a JVM
which is shared among all the classes. This holds the class
level information of each .class file.
• Heap: Heap is a part of JVM memory where objects are
allocated. JVM creates a Class object for each .class file.
• Stack: Stack is a also a part of JVM memory but unlike
Heap, it is used for storing temporary variables.
• PC Registers: This keeps the track of which instruction has
been executed and which one is going to be executed. Since
instructions are executed by threads, each thread has a
separate PC register.
• Native Method stack: A native method can access the
runtime data areas of the virtual machine.
• Native Method interface: It enables java code to call or
be called by native applications. Native applications are
programs that are specific to the hardware and OS of a
system.
• Garbage collection: A class instance is explicitly created
by the java code and after use it is automatically
destroyed by garbage collection for memory
management.
• Simple Java Program

public class FirstJavaProgram {


public static void main(String[] args){
System.out.println("This is my first program in java");
}//End of main
}//End of FirstJavaProgram Class
• Use Notepad
• Save as FirstJavaProgram.java
• Use cmd

javac FirstJavaProgram.java
If got error “javac’ is not recognized as an internal or
external command, operable program or batch file“.

set path=C:\Program Files\Java\jdk-11.0.2\bin


java FirstJavaProgram
• Abstraction is an act to represent real world
object into software component.
• It is a process to identify objects and classes
for the system.
• Object is the main component in object
oriented programming.
• An object can be considered as a “thing”
that will do a set of related activities
• Object has Identity
– What makes an object different from another object?

• Object has State


– What is the data of the object?

• Object has Behavior


– What the object can do?
– What can we do with the object?
• In object oriented programming,
– Objects are created
– Classes are defined

• Object is an instance of a class

• A class is a general definition for a group of objects


that has similar characteristics and behaviour.
Compartment for instance variables and methods can be hide
• A class must have a Name
– What is the unique name for a group of object?

• A class should have Instance variables


– What the class should know?
– What is the data or attribute of the class?

• A class should have Method


– How to class act and react?
– What is the name of the action and reaction?
• If you notice, instance variables and methods are
• packaged together in a class.

• This is because it is not logical to separate


data/information and method/functions from
a class.
• –Because in real world, data and functions are
not separated into different packages.

• The class model must reflects the real world entity.


• Real world object vary in complexity
• Some objects has similar behavior but not the same
– Lamp (on, off, turnOn, turnOff)
– Radio (on, off, frequency, volume, turnOn, turnOff, tuning, increaseVolume,
decreaseVolume)

• Some objects are extended from another object


– Student (name, cgpa, takeExam())
– PostGraduate (researchArea, supervisor, presentProgress())
• PostGraduate is an extended class from student

• Some objects contains another objects


– Car (name, model, door, color, engine)
– Engine (model, capacity)
• Engine is an object resides in a car
Basic frame of Program

This is a Java comment. It is


// This is a simple Java program. ignored by the compiler.

public class Simple This is the class header


for the class Simple
{

This area is the body of the class Simple.


All of the data and methods for this class
will be between these curly braces.

2-29
Basic frame of Program

This is the method header


// This is a simple Java program. for the main method. The
main method is where a Java
public class Simple application begins.
{
public static void main(String[] args)
{ This area is the body of the main method.
All of the actions to be completed during
the main method will be between these curly braces.
}
}

2-30
Basic frame of Program

// This is a simple Java program.

public class Simple


{
public static void main(String [] args)
{
System.out.println("Programming is great fun!");
}
}
This is the Java Statement that
is executed when the program runs.

2-31
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!"); Source Code
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result


Enter main method

//This program prints Welcome to Java!


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

34
Execute statement

//This program prints Welcome to Java!


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

35
//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

36
• Class name
• Main method
• Statements
• Statement terminator
• Reserved words
• Comments
• Blocks

37
Every Java program must have at least one class. Each class
has a name. By convention, class names start with an
uppercase letter. In this example, the class name is
Welcome.

//This program prints Welcome to Java!


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

38
Line 2 defines the main method. In order to run a class, the
class must contain a method named main. The program is
executed from the main method.

//This program prints Welcome to Java!


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

39
A statement represents an action or a sequence of
actions. The statement System.out.println("Welcome to
Java!") to display the greeting "Welcome to Java!“.

//This program prints Welcome to Java!


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

40
Every statement in Java ends with a semicolon (;).

//This program prints Welcome to Java!


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

41
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.
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

42
Reserved words
abstract do implements public true
assert double import return try
boolean else instanceof short void
break enum int static volatile
byte extends interface strictfp while
case false long super default
catch final native switch if
char finally new synchronized protected
class float null this transient
const for package throw
continue goto private throws
A pair of braces in a program forms a block that groups
components of a program.

public class Test {


public static void main(String[] args) { Class block
System.out.println("Welcome to Java!"); Method block
}
}

44
Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.

45
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

46
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

47
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

48
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

49
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

50
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result

51
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}

52
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}

53
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}

54

You might also like