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

Lecture - 1 Introduction to Java

Uploaded by

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

Lecture - 1 Introduction to Java

Uploaded by

Subham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Topics to be Covered

• Introduction to Object-Oriented programming


• Features of Object-Oriented Programming
• Features of Java
• Difference between C++ and Java
• JDK, JRE, JVM
• JVM – Java Virtual Machine Architecture
• Steps for compiling and executing a Java program
Introduction to
Object Oriented Programming
Introduction to Object Oriented Programming

• There are three types of programming methodology :


• Monolithic Programming
• Procedure-oriented Programming
• Object-oriented Programming

• Monolithic Programming
• It indicates the program which contains a single function for a large program.
• A program is not divided into parts and hence is the name.
• When the program size increases it leads inconvenience and is difficult to maintain.
• The global data can be accessed and modified from any part of the program and hence posing a
serious threat.
• It is suitable to develop simple and small applications.
Example: Basic
Introduction to Object Oriented Programming
• Procedure Oriented Programming
• It basically consists of writing a list of instructions organized into groups known as functions.
• The primary focus is on function rather than data.
• Larger programs are divided into smaller programs known as functions.
• Most of the functions share global data.
• Data moves openly around the system from function to function.
• Examples: COBOL, Fortran, C

Structure of Procedure-oriented Programming Relationship of Data and Functions


Introduction to Object Oriented Programming
• Object Oriented Programming
• OOP allows the decomposition of a problem into a number of entities called objects and then builds data and
functions around these objects.
• It treats data as a critical element in program development and does not allow it to flow freely around the
system.
• It ties data more closely to the functions that operate on it and protects it from accidental modification from
outside functions.
• The data of an object can be accessed only by the functions associated with that object.
• Data is hidden and cannot be accessed by external functions.
• Objects may communicate with each other through functions.
Example: C++, Java etc

Organization of data and function in OOP


Features of
Object Oriented Programming
Features of Object Oriented Programming

• The following are the basic features of Object Oriented Programming:

• Object
• Class
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Features of Object Oriented Programming

Object:

• Any entity that has a state and behavior is known


as an object.
• Example: A dog is an object because it has states
like color, name, breed, etc. as well as behaviors like
wagging the tail, barking, eating, etc.
• An Object can be defined as an instance of a class.
An object contains an address and takes up some
space in memory.
Features of Object Oriented Programming

Class:
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which you can create an individual object.
Class doesn't Abstraction
Abstraction:
• Hiding internal details and showing functionality is known as abstraction. For example phone
call, we don't know the internal processing.
• In Java, we use abstract classes and interfaces to achieve abstraction.
Encapsulation:
• Binding (or wrapping) code and data together into a single unit are known as encapsulation.
For example, a capsule, it is wrapped with different medicines.
• A Java class is an example of encapsulation.
Features of Object Oriented Programming
Inheritance:
• When one object acquires all the properties and behaviors of a parent object, it is known as
inheritance.
Polymorphism:
• If one task is performed in different ways, it is known as polymorphism. For example: to
convince the customer differently, to draw something, for example, shape, triangle,
rectangle, etc.
• In Java, we use method overloading and method overriding to achieve polymorphism.
• Another example can be to speak something; for example, a cat speaks meow, a dog barks
woof, etc.
Features of Java
Features of Java
Features of Java:
• A list of the most important features of the Java language is given below.
1) Object-Oriented
2) Simple
3) Secured
4) Platform independent
5) Robust
6) Portable
7) Architecture Neutral
8) Dynamic
9) Interpreted
10) High Performance
11) Multithreaded
12) Distributed
Features of Java
1) Object Oriented:
• Java is an object-oriented programming language. Everything in Java is an object. Object-
oriented means we organize our software as a combination of different types of objects that
incorporate both data and behavior.
• Object-oriented programming (OOPs) is a methodology that simplifies software
development and maintenance by providing some rules.

2) Simple:
• Java is very easy to learn, and its syntax is simple, clean, and easy to understand.
• Java syntax is based on C++ (so easier for programmers to learn it after C++).
• Java has removed many complicated and rarely-used features, for example, explicit pointers,
operator overloading, etc.
• There is no need to remove unreferenced objects because there is an Automatic Garbage
Collection in Java.
Features of Java
3) Secured:
• Java is best known for its security. With Java, we can develop virus-free systems.
• No explicit pointer
• Java Programs run inside a virtual machine sandbox
Features of Java
4) Platform Independent:
• A platform is the hardware or software environment in which a program runs.
• Java is platform-independent because, Java code can be executed on multiple platforms, for
example, Windows, Linux, Sun Solaris, Mac/OS, etc.
• Java code is compiled by the compiler and converted into bytecode (class file). This bytecode
is a platform-independent code because it can be run on multiple platforms, i.e., Write Once
and Run Anywhere (WORA).
Features of Java
5) Robust:
• It uses strong memory management.
• Java provides automatic garbage collection which runs on the Java Virtual Machine to get
rid of objects which are not being used by a Java application anymore.
• There are exception handling and type-checking mechanisms in Java.

6) Portable:
• Java is portable because it facilitates you to carry the Java bytecode to any platform. It
doesn't require any implementation.
7) Architectural Neutral:
• Java is architecture-neutral because there are no implementation-dependent features, for
example, the size of primitive types is fixed.
• In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4
bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32
and 64-bit architectures in Java.
Features of Java
8) Dynamic:
• Java is a dynamic language. It supports the dynamic loading of classes. It means classes are
loaded on demand. It also supports functions from its native languages, i.e., C and C++.
• Java supports dynamic compilation and automatic memory management (garbage
collection).
9) Interpreted:
• Java is both a compiled and interpreted language.
• Java code is first compiled into byte-code and later it is interpreted by JVM for execution.

10) High Performance:


• Java is faster than other traditional interpreted programming languages because Java
bytecode is "close" to native code.
• It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted
language that is why it is slower than compiled languages, e.g., C, C++, etc..
Features of Java
11) Multithreaded:
• A thread is like a separate program, executing concurrently.
• We can write Java programs that deal with many tasks at once by defining multiple threads.
• The main advantage of multi-threading is that it doesn't occupy memory for each thread. It
shares a common memory area.
• Threads are important for multi-media, Web applications, et.

12) Distributed:
• Java is distributed because it facilitates users to create distributed applications in Java.
• RMI and EJB are used for creating distributed applications.
• This feature of Java makes us able to access files by calling the methods from any machine
on the internet.
Difference between
C++ and Java
Difference between C++ and Java
Comparison Index C++ JAVA

Platform-independent C++ is platform-dependent. Java is platform-independent.

Goto C++ supports the goto statement. Java doesn’t support the goto statement.

C++ supports pointers. You can write a Java supports pointer internally. However, you
Pointers pointer program in C++. can't write the pointer program in java. It means
java has restricted pointer support in java.

Operator Overloading C++ supports operator overloading. Java doesn't support operator overloading.

Multiple inheritance C++ supports multiple inheritance. Java doesn't support multiple inheritance through
class. It can be achieved by using interfaces in java.

Structure and Union C++ supports structures and unions. Java doesn't support structures and unions.

Thread Support C++ doesn't have built-in support for Java has built-in thread support.
threads.
JDK, JRE and JVM
JDK, JRE and JVM
JDK, JRE and JVM
JDK
• JDK is an acronym for Java Development Kit.
• The Java Development Kit (JDK) is a software development environment that is used to
develop Java applications and applets. It physically exists and it contains JRE + development
tools.
• JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
• Standard Edition Java Platform
• Enterprise Edition Java Platform
• Micro Edition Java Platform
• The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), etc. to complete the development of a Java Application.
JDK, JRE and JVM
JRE
• JRE is an acronym for Java Runtime Environment.
• It is also written as Java RTE.
• The Java Runtime Environment is a set of software tools that are used for
developing Java applications.
• It is used to provide the runtime environment. It is the implementation of JVM.
• It physically exists. It contains a set of libraries + other files that JVM uses at
runtime.
JDK, JRE and JVM
JVM
• JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine
because it doesn't physically exist.
• It is a specification that provides a runtime environment in which Java bytecode can be
executed. It can also run those programs which are written in other languages and
compiled to Java bytecode.
• JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are
platform dependent because the configuration of each OS is different from each other.
• The JVM performs the following main tasks:
• Loads code
• Verifies code
• Executes code
Java Virtual Machine
( JVM) Architecture
JVM Architecture
• JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime
environment in which Java bytecode can be executed.
• JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).
JVM Architecture
1) Classloader:
• Classloader is a subsystem of JVM that is used to load class files. Whenever we run the java
program, it is loaded first by the classloader. .
2) Class Area:
• Class(Method) Area stores per-class structures such as the runtime constant pool, field, and
method data, and the code for methods.
3) Heap:
• It is the runtime data area in which objects are allocated.

4) Stack:
• Java Stack stores frames. It holds local variables and partial results and plays a part in method
invocation and return.
5) PC Register:
• PC (program counter) register contains the address of the Java virtual machine instruction
currently being executed.
JVM Architecture
6) Native Method Stack:
• It contains all the native methods used in the application.
7) Execution Engine:
It contains:
• A virtual processor
• Interpreter: Read bytecode stream then execute the instructions.
• Just-In-Time(JIT) compiler:
• It is used to improve performance.
• JIT compiles parts of the byte code that have similar functionality at the same time and hence reduces the
amount of time needed for compilation.
• Here, the term "compiler" refers to a translator from the instruction set of a Java virtual machine (JVM) to
the instruction set of a specific CPU.

8) Java Native Interface:


• Java Native Interface (JNI) is a framework that provides an interface to communicate with
another application written in another language like C, C++, Assembly etc. Java uses the JNI
framework to send output to the Console or interact with OS libraries.
Steps to compile and
run a Java Program
Steps to compile and run a Java Program

To create a simple Java program, you need to create a class that contains the main method.

Let's understand the requirement first.

The requirement for printing Hello World :

• Install the JDK (https://www.oracle.com/java/technologies/downloads/)


• Set path of the jdk/bin directory.
• Create the Java program
• Compile and run the Java program
Steps to compile and run a Java Program
Open NOTEPAD application and type the following code:

class Simple
{
public static void main(String args[])
{
System.out.println("Hello Java");
}
}
-> Save the file Simple.java ( Assume it is saved in D:/Jprogs/Simple.java)
-> Open Command Prompt and goto D:\Jprogs
-> Type javac Simple.java ( To compile the program)
-> Type java Simple ( To execute the program )
Steps to compile and run a Java Program
In the above program :
1. class keyword is used to declare a class in Java.

2. public keyword is an access modifier that represents visibility. It means it is visible to all.

3. static is a keyword. If we declare any method as static, it is known as the static method. The core advantage of
the static method is that there is no need to create an object to invoke the static method. The main() method is
executed by the JVM, so it doesn't require creating an object to invoke the main() method. So, it saves memory.

4. void is the return type of the method. It means it doesn't return any value.

5. main represents the starting point of the program.

6. String[] args or String args[] is used for command line argument.

7. System.out.println() is used to print statement.


 Here, System is a class
 out is an object of the PrintStream class,
 println() is a method of the PrintStream class.
Thank You

You might also like