Lecture - 1 Introduction to Java
Lecture - 1 Introduction to Java
• 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
• Object
• Class
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Features of Object Oriented Programming
Object:
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.
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
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.
To create a simple Java program, you need to create a class that contains the main method.
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.