0% found this document useful (0 votes)
13 views

Lecture 3

The document provides an overview of Java as a general-purpose, object-oriented programming language, detailing its compilation process from source code to bytecode execution on the Java Virtual Machine (JVM). It explains the structure of a Java program, including components such as classes, methods, constructors, and variable types, along with access control modifiers. The content is aimed at educating students on the fundamental aspects of Java programming as part of an Object-Oriented Programming course.

Uploaded by

imranjani395988
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 3

The document provides an overview of Java as a general-purpose, object-oriented programming language, detailing its compilation process from source code to bytecode execution on the Java Virtual Machine (JVM). It explains the structure of a Java program, including components such as classes, methods, constructors, and variable types, along with access control modifiers. The content is aimed at educating students on the fundamental aspects of Java programming as part of an Object-Oriented Programming course.

Uploaded by

imranjani395988
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture: 3 (Structure and

Compilation process of Java)


Course

Object Oriented
Programming (OOP)

Instructor

Mr. Sarmad Shafique


Lecturer
Department of Computer Science
National University of Modern Languages
What is Java?
General Purpose and Powerful Object-Oriented Language
programming language.
Based on C/C++
Used for developing software that run on mobile, desktop and
servers.
Machine Independent (Write Once Run Anywhere (WORA))
Java is known for its platform independence, robustness, and
wide adoption in various domains, including web development,
mobile app development, enterprise software, and more.

2
Java Compilation Process
The Java compilation process is the series of steps that occur when you transform human-
readable Java source code into bytecode, which can be executed by the Java Virtual Machine
(JVM).
This process involves several stages:
Writing Java Source Code:
The first step is to write your Java source code in a plain text file. This code contains your program's
logic and is written in the Java programming language.
Compilation (javac):
The Java compiler, javac, is responsible for translating the Java source code into bytecode.
You run the javac command followed by the name of the Java source file (with the .java extension).
For example: javac MyProgram.java
If the code contains errors, the compiler will report them as compilation errors, and you'll need to fix
them before proceeding.
Bytecode Generation:
If there are no compilation errors, the Java compiler generates bytecode instructions.
These bytecode instructions are platform-independent and can be executed on any device that has a
Java Virtual Machine (JVM).
3
Java Compilation Process (Continued)
Class File Creation:
The compiler creates one or more binary files with a .class extension, where each .class file corresponds
to a class in your Java source code.
These .class files contain the bytecode for your classes and are used for execution.
Execution (java):
To run your Java program, you use the java command followed by the name of the class containing the
main method (the entry point of your program) but without the .class extension.
For example: java MyProgram
The JVM loads and executes the bytecode from the .class files.
It also performs Just-In-Time (JIT) compilation, which translates the bytecode into machine code for the
specific hardware and operating system.
Execution of the main Method:
The main method of the specified class is executed, starting the program's execution.
Termination:
Your program runs until it completes its tasks or encounters an error. At this point, it terminates, and the
JVM releases any allocated resources.
4
Java Compilation Process (Continued)

5
Structure of Java Program
Import Statements (if needed): At the
beginning of your Java program, you can include
import statements to bring in classes from other
packages or libraries that your program will use.
These statements are not always required.
Class Definition: The program begins with
the definition of a Java class. The class acts as a
blueprint for objects, defining their attributes and
behavior.
Instance Variables (Attributes): Within the
class, you can declare instance variables (also
called attributes) that represent the state of
objects created from the class.
Constructors: Constructors are special
methods with the same name as the class. They
initialize the objects created from the class. You
can have multiple constructors, including
overloaded constructors with different parameter
lists. Constructors are called when you create an
object using the new keyword.
6
Structure of Java Program
Methods (Functions): Inside
the class, you can define
methods that represent the
behavior or actions that objects
of this class can perform. These
methods can have parameters
and return values.

Main Method: The main


method is the entry point of the
Java program. It's a special
method that allows your program
to be executed. It takes an array
of strings (args) as a parameter,
which can be used to pass
command-line arguments to the
program.
7
Methods
A method in Java is a block of code within a class that performs a specific task
or action.
Methods are used to encapsulate behavior within objects and promote code
reusability.
Methods can have zero or more parameters and may return a value.
Methods are declared within a class using the following syntax:
returnType methodName(parameter1Type parameter1Name, parameter2Type parameter2Name, ...)
{
// Method body
// Perform actions here
return returnValue; // Optional return statement
}
8
Methods Signature
Simple Method

In Java, a method signature is a unique


identifier for a method within a class. It
consists of two main components:
Method Name:
This is the name given to the method, which
identifies it within the class. The method
name is case-sensitive. Overloaded Method
Parameter List:
The parameter list includes the number,
order, and data types of the method's
parameters. It specifies what kind of input the
method expects when it is called.
Method overloading is possible when
methods have the same name but
different parameter lists.
9
Methods Arguments and Parameters
Parameters:
Parameters are variables declared
in the method signature to accept
values from the method caller.
Arguments:
Arguments are actual values
passed to a method when it is
called, corresponding to the
parameters.
Here's an example with diagrams to
illustrate these concepts:

10
Classes and Objects
Object - Objects have states and behaviors.
Example: A dog has states - color, name, breed as
well as behaviors -wagging, barking, eating. An object
is an instance of a class.
Class - A class is a blueprint or template for creating
objects. It defines the structure and behavior of
objects of that class.

11
Class Example
public class Dog{
String breed;
int age;
String color;
void barking(){ }
void hungry(){ }
void sleeping(){ }
}
12
Constructor
Constructors have the same name as the class
No return type
Every class has a constructor.
If we do not explicitly write, Java compiler builds a
default constructor for that class.
Each time a new object is created, at least one
constructor will be invoked.
A class can have more than one constructor.
13
Constructor
public class Employee{
public Employee(){ }
public Employee(String name){
// This constructor has one parameter, name. }
}

14
Creating an Object
Declaration: A variable declaration with a variable
name with an object type.
Instantiation: The 'new' key word is used to create
the object.
Initialization: The 'new' keyword is followed by a
call to a constructor. This call initializes the new
object.

16
Creating an Object
public class Cat{
public Cat(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}

public static void main(String []args){


// Following statement would create an object myCat
Cat myCat = new Cat( “Kitty" );
}

}
17
Variable types
Local variables:
defined inside methods, constructors or blocks
destroyed when the method has completed.
Instance variables:
variables within a class but outside any method.
instantiated when the class is loaded.
can be accessed from inside any method, constructor or blocks of that particular
class.
Class variables:
declared with in a class, outside any method, with the static keyword.
Static variables can be accessed by calling with the class name .
ClassName.VariableName.
18
Local Variables
public class Test{
public void pupAge(){
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.pupAge();
}
}
19
Instance Variables
import java.io.*;
public class Employee{// this instance variable is visible for any child class.
public String name; // salary variable is visible in Employee class only.
private double salary; // The name variable is assigned in the constructor.
public Employee (String empName){
name = empName;
}
public void setSalary(double empSal){// The salary variable is assigned a value.
salary = empSal;
}
public void printEmp(){// This method prints the employee details.
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]){
Employee empOne = new Employee(“Ali"); empOne.setSalary(1000); empOne.printEmp();
}
} 20
Class/Static Variable
import java.io.*;
public class Employee{ // salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static String DEPARTMENT = "Development“ ;
public static void main(String args[]){
salary = 1000;
System.out.println(Employee.DEPARTMENT+"average salary:"+
Employee.salary);
}
21
Java Modifier Type
Access Control Modifiers:
Access control modifiers determine the visibility and accessibility of classes, methods, and fields
from different parts of your Java program. There are four main access control modifiers in Java:
public: Members with this modifier are accessible from anywhere, both within the same package and
from outside the package.
protected: Members with this modifier are accessible within the same package and by subclasses,
whether they are in the same package or not.
private: Members with this modifier are accessible only within the same class. They are not visible from
outside the class.
default (package-private): If no access modifier is specified (i.e., no modifier is used), the member is
considered "package-private." It is accessible only within the same package.

Non Access Modifiers:


The static modifier for creating class methods and variables
The final modifier for finalizing the implementations of classes, methods, and variables.
The abstract modifier for creating abstract classes and methods.
The synchronized and volatile modifiers, which are used for threads. 22
Access Modifier
Access Modifier Class or member can be
referenced by…
public methods of the same class, and
methods of other classes
private methods of the same class only

protected methods of the same class, methods


of subclasses, and methods of
classes in the same package
No access modifier methods in the same package only
(package access)
23
Thank You
Any Questions?

24

You might also like