0% found this document useful (0 votes)
4 views41 pages

Introduction To Java

Uploaded by

Bonny runameso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views41 pages

Introduction To Java

Uploaded by

Bonny runameso
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit 1_OOP in Java

Introduction
Ruvinga
Contents
• Introduction to Java
• Java Key words
• Java Features
• JVM machine
• JVM architecture
• JVM Vs JRE Vs JDK
• First Java program
Introduction to JAVA
• JAVA developed by James Gosling and Patrick Naughton Sun
Microsystems Inc in 1991,

• JAVA is a simple programming language that enables you to write,


compile and debug a program easily

• JAVA helps to create modular programs and reusable code.


JAVA keywords
Java Virtual Machine (JVM)
JVM’s primary function is to execute the bytecode produced by compiler.
.
The phases of program execution include
1. Program writing - done by java programmer
2. Program compilation-done by javac compiler
NB. javac is the primary java compiler included in java development kit (JDK)
3. Runnning the program- JVM executes the bytecode generated by compiler .
NB Java is a platform independent language because JVM is different on each operating system
but the output produced after execution of bytecode is same across all operating systems
Bytecode
• is program code that has been compiled from source code into low-level
code designed for a software interpreter. It may be executed by a virtual
machine (such as a JVM)
• javac compiler of JDK compiles the java source code into bytecode so that it
can be executed by JVM.
• the bytecode is saved in a .class file by compiler.

Java Development Kit(JDK)

• this is complete java development kit that includes JRE (Java Runtime
Environment), compilers and various tools like JavaDoc, Java debugger etc.
• In order to create, compile and run Java program you need JDK installed on
your computer.
Java Runtime Environment(JRE)

• JRE is a part of JDK which means that JDK includes JRE.


• When you have JRE installed on your system, you can run a java
program however you won’t be able to compile it.
• JRE includes JVM, browser plugins and applets support.
• When you only need to run a java program on your computer, you
would only need JRE.
Java program execution
JAVA 8 Main Features
1. Java is a platform independent language
• Compiler(javac) converts source code (.java file) to the byte
code(.class file).
• JVM executes the bytecode produced by compiler. This byte code
can run on any platform such as Windows, Linux, Mac OS etc.
• Which means a program that is compiled on windows can run on
Linux and vice-versa.
• Each operating system has different JVM, however the output they
produce after execution of bytecode is same across all operating
systems. That is why we call java as platform independent language.
[Link] is an Object Oriented language
Object oriented programming is a way of organizing programs as
collection of objects, each of which represents an instance of a class.
4 main concepts of Object Oriented programming are:
• Abstraction
• Encapsulation
• Inheritance
• polymophism
The concepts will be discussed in detail later
3. Simple
Java is considered as one of simple language because it does not have
complex features like Operator overloading,multiple inheritance,
pointers and Explicit memory allocation.
4. Robust Language
Robust means reliable. Java programming language is developed in a
way that puts a lot of emphasis on early checking for possible errors,
that’s why java compiler is able to detect errors that are not easy to
detect in other programming languages. The main features of java that
makes it robust are garbage collection, Exception Handling and
memory allocation.
[Link]
We don’t have pointers and we cannot access out of bound arrays (you
get ArrayIndexOutOfBoundsException if you try to do so) in java. That’s
why several security flaws like stack corruption or buffer overflow is
impossible to exploit in Java.
6. Java is distributed
Using java programming language we can create distributed
applications. RMI(Remote Method Invocation) and EJB(Enterprise Java
Beans) are used for creating distributed applications in java. In simple
words: The java programs can be distributed on more than one
systems that are connected to each other using internet connection.
Objects on one JVM (java virtual machine) can execute procedures on a
remote JVM.
7. Multithreading
Java supports multi threading. Multithreading is a Java feature that
allows concurrent execution of two or more parts of a program for
maximum utilisation of CPU.
8. Portable
java code that is written on one machine can run on another machine.
The platform independent byte code can be carried to any platform for
execution that makes java code portable.
Java Virtual Machine (JVM)

• The Java Virtual machine (JVM) is the virtual machine that runs on
actual machine (your computer) and executes Java byte code.
• The JVM doesn’t understand Java source code, that’s why we need to
have javac compiler that compiles *.java files to obtain *.class files
that contain the byte codes understood by the JVM.
• JVM makes java portable (write once, run anywhere). Each operating
system has different JVM, however the output they produce after
execution of byte code is same across all operating systems.
JVM Architecture
• 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.
JVM Vs JRE Vs JDK
• JRE: is the environment within which the java virtual machine runs.
JRE contains Java virtual Machine(JVM), class libraries, and other files
excluding development tools such as compiler and debugger.
Which means you can run the code in JRE but you can’t develop and
compile the code in JRE.
• JVM: runs the program by using class, libraries and files provided by
JRE.
• JDK: JDK is a superset of JRE, it contains everything that JRE has along
with development tools such as compiler, debugger etc.
How to Compile and Run your First Java Program

In order to write and run a Java program, you need to install


a software program called Java SE Development Kit (or JDK for short,
and SE means Standard Edition).
JDK contains: JRE(Java Runtime Environment): the core of
the Java platform that enables running Java programs on your
computer.
Download and install Java Development Kit
Every Java program starts from the main() method
public class FirstJavaProgram {

• Its the first line of our java program


• Every java application must have at least one class definition that
consists of class keyword followed by class name.
• keyword, means that it should not be changed, we should use it as
it is.
• class name can be anything.
• the class has a public access modifier
• a java file can have any number of classes but it can have only one
public class and the file name should be same as public class name.
public static void main(String[] args) {
public: makes the main method public i.e we can call the method from
outside the class.
static: We do not need to create object for static methods to run. They can
run itself.
void: It does not return anything.
main: It is the method name. This is the entry point method from which the
JVM can run your program.
(String[] args): Used for command line arguments that are passed as
strings.
[Link](“Hello world");

• the method prints the contents inside the double quotes into the
console and inserts a newline after.
Summary
• JDK is the Java SE Development Kit that contains tools and libraries for Java
development.
• JRE is the Java Runtime Environment that enables running Java programs on
your computer.
• JVM is the Java Virtual Machine that actually executes Java programs. With
JVM, programs written in Java can run on multi-platforms (thus Java is called
cross-platform language).
• javac is the Java compiler. It translates Java source code into bytecode.
• java is the JVM launcher which we use to run our program.
• Every Java program starts from the main() method.
• When compiling, the compiler generates a .class file from [Link] file.
Review questions
• What is JAVA?
• What makes java platform independent?
• Draw JVM Architecture.
• Can you develop and compile code in JRE?
Lab Exercise 1
Write HelloWorld program
References
• [Link]
• [Link]
• [Link]
• [Link]
• Java The Complete Reference Ninth Edition, Herbert Schildt,
McGraw-Hill Education, 2014.
• Java 8 Pocket Guide by Robert Liguori and Patricia Liguori, 2014.

You might also like