Java Programming
Unit - 1
What is Java
Java is an object-oriented programming language
developed by Sun Microsystems, a company best
known for its high-end Unix workstations. Modeled
after C++, the Java language was designed to be
small, simple, and portable across platforms and
operating systems.
Java is also a platform.
Any hardware or software environment in which
a program runs, is known as a platform. Since
Java has its own runtime environment (JRE) and
API, it is called platform.
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side
standalone applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side
applications such as Java servlets and Java
ServerPages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for
mobile devices such as cell phones.
JDK vs JRE vs JVM
JDK (Java Development Kit)
JDK contains everything that will be required to develop
and run Java application.
JRE (Java Run time Environment)
JRE contains everything required to run Java application
which has already been compiled. It doesn’t contain the
code library required to develop Java application.
JVM (Java Virtual Machine)
JVM is a virtual machine which work on top of your
operating system to provide a recommended environment
for your compiled Java code. JVM only works with
bytecode. Hence you need to compile your Java
application(.java) so that it can be converted to bytecode
format (also known as the .class file). Which then will be
used by JVM to run application.
JDK vs JRE vs JVM
Java Development Kit (JDK) consists of Java
Runtime Environment (JRE) along with tools
to compile and debug Java code for
developing Java applications.
JRE consists of libraries, Java Virtual Machine
(JVM), Java Plug-in and Java Web Start to
run Java applications.
JRE as a stand-alone does not contain
compilers and debugging tools.
JVM (Java Virtual Machine)
JVM is a virtual machine or a program that provides
run-time environment in which java byte code can
be executed. JVMs are available for many
hardware and software platforms. The use of the
same byte code for all JVMs on all platforms make
java platform independent.
Main task of JVM:
1. Search and locate the required files.
2. Convert byte code into executable code.
3. Allocate the memory into ram
4. Execute the code.
5. Delete the executable code
Java better than C++ ?
No Typedefs, Defines, or Preprocessor
No Global Variables
No Goto statements
No Pointers
No Unsafe Structures
No Multiple Inheritance
No Operator Overloading
No Automatic Coercions
Java Buzzwords
Simple:
Java is a simple Language because it contains
many features of other Languages like C and
C++ and removes complexity because it
doesn’t support pointers, storage classes, goto
statements and multiple inheritances.
Object oriented:
Java is purely an Object Oriented Programming
language i.e., all the code of the Java
language is written into the classes and objects.
Java Buzzwords
Distributed:
Java is a distributed language because,
because of its ability to share the data and
programs over the LAN.
Multithreaded:
A Java program can be divided into multiple
threads assigning different tasks for different
threads and have all the threads executing in
parallel.
Java Buzzwords
Dynamic:
The JVM maintains a lot of runtime information
about the program and the objects in the
program. Libraries are dynamically linked during
runtime.
Architecture Neutral:
Java follows “Write-once-run-anywhere”
approach. Java programs are compiled into
byte code format which does not depend on
any machine architecture but can be easily
translated into a specific machine by a JVM for
that machine.
Java Buzzwords
Portable:
in Java the size of the primitive data types are
machine independent. For example, an int in
Java is always a 32-bit integer, and float is
always a 32-bit IEEE 754 floating point number.
High performance:
Java programs are compiled to portable
intermediate form known as byte codes, rather
than to native machine level instructions and
JVM executes the byte codes on any machine
on which it is installed.
Java Buzzwords
Robust:
A Program or an application is said to be robust
(reliable) when it is able to give some response
in any kind of context. Java’s features help to
make the programs robust. Some of those
features are: type checking, exception
handling, etc
Secured:
Java provides data security through
encapsulation.
Types of Java Applications
Standalone Application
Itis also known as desktop application or window-
based application. An application that we need
to install on every machine such as media player,
antivirus etc. AWT and Swing are used in java for
creating standalone applications.
Web Application
An application that runs on the server side and
creates dynamic page, is called web
application. Currently, servlet, jsp, struts, jsf etc.
technologies are used for creating web
applications in java.
Types of Java Applications
Enterprise Application
An application that is distributed in nature, such
as banking applications etc. It has the
advantage of high level security, load balancing
and clustering. In java, EJB is used for creating
enterprise applications.
Mobile Application
An application that is created for mobile devices.
Currently Android and Java ME are used for
creating mobile applications.
An Overview of Java
Object-Oriented Programming:
Object-oriented programming (OOP) is at
the core of Java. In fact, all Java programs
are to at least some extent object-oriented.
Two Paradigms:
1. Process-Oriented Model
2. Object-oriented programming
An Overview of Java
Process-Oriented Model: This approach
characterizes a program as a series of linear
steps (that is, code). The process-oriented
model can be thought of as code acting on
data.
Object-oriented programming: This
approach organizes a program around its
data (that is, objects) and a set of well-
defined interfaces to that data. An object-
oriented program can be characterized as
data controlling access to code.
Abstraction
Hiding the implementation details from the
user, only the functionality will be provided
to the user.
We use abstract class and interface to
achieve abstraction.
Ex: The article they write on newspaper is
abstracted as the heading.
Three OOP Principles
Inheritance :
When one object acquires all the properties
and behaviors of parent object i.e. known as
inheritance.
It provides code reusability.
It is used to achieve runtime polymorphism.
Ex:Father has a relation with his Grandfather
and Children have relation with their Father.
Three OOP Principles
Polymorphism
When one task is performed by different
ways.
In
java, we use method overloading and
method overriding to achieve polymorphism.
Ex: Your mobile phone, one name but many
forms: as phone, as camera, as mp3 player,
as radio
Three OOP Principles
Encapsulation:
Binding (or wrapping) code and data
together into a single unit is known as
encapsulation.
Ex:capsule, it is wrapped with different
medicines.
A java class is the example of encapsulation.
First Java Program
public class HelloWorld
{
public static void main (String args[])
{
System.out.println("Hello World");
}
}
First Java Program
The keyword 'public' says that, the main
function can be accessed even outside the
defining class.
The keyword 'static' indicates that a function
can be accessed without an object.
First Java Program
The keyword 'void' indicates that a function
returns nothing.
The Keyword 'main' is the name of the
function and entry point for a Java
application.
Instance Variables and
Class Variables
Variables within the class created without
the keyword 'static' prefixed are called
instance variables or object variables or non-
static variables.
A separate memory space for these types of
variables will be created for each instance
of a class.
Instance Variables and
Class Variables
Variables within the class created with the
keyword 'static' prefixed are called class
variables or static variables.
This type of variables actually shares the
value among all the instances of a class.
Instance variables declared as static are,
essentially, global variables.
Understanding static
Methods declared as static have several
restrictions:
Theycan only directly call other static
methods.
They can only directly access static data.
They cannot refer to this or super in any way.
Understanding static
Understanding static
Introducing final
A field can be declared as final.
Doing so prevents its contents from being
modified, making it, essentially, a constant.
This means that you must initialize a final field
when it is declared.
You can do this in one of two ways:
First,
you can give it a value when it is declared.
Second, you can assign it a value within a
constructor.
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
Introducing Nested and Inner
Classes
It is possible to define a class within another
class; such classes are known as nested
classes.
The scope of a nested class is bounded by the
scope of its enclosing class.
Thus, if class B is defined within class A, then B
does not exist independently of A.
A nested class has access to the members,
including private members, of the class in
which it is nested.
Introducing Nested and Inner
Classes
However, the enclosing class does not have
access to the members of the nested class.
A nested class that is declared directly within
its enclosing class scope is a member of its
enclosing class.
It is also possible to declare a nested class that
is local to a block.
There are two types of nested classes: static
and non-static.
Introducing Nested and Inner
Classes
The most important type of nested class is the
inner class.
An inner class is a non-static nested class.
It has access to all of the variables and
methods of its outer class and may refer to
them directly in the same way that other non-
static members of the outer class do.
Non-Static Inner Classes
Static Inner Classes
Stack Implementation
Stack Implementation
Inheritance
The process of obtaining properties of one entity in
another entity is known as Inheritance.
“extends” keywords is used for the purpose of
inheritance.
A class can only extend “only one class”.
Inheritance can also be referred to the concept of
Generalization, which is the process of extracting
common characteristics (states and behavior)
from two or more classes, and combining them
into a generalized super class.
Inheritance
Constructors
It is a special method will be automatically
called at the time of instantiating the class.
Rules:
It must have same name as that of class name.
It doesn’t contain any return type even void also.
If
we want we can provide access specifiers like
default, protected and public other than private.
By using any parameterized constructor in our
class, if we want to create instance for the class
by using default constructor then we need to
provide default constructor implementation
explicitly in the class.
Using super
super has two general forms.
The first calls the superclass’ constructor.
Using super
The second is used to access a member of
the superclass that has been hidden by a
member of a subclass.
Method Overriding
In a class hierarchy, when a method in a
subclass has the same name and type signature
as a method in its superclass, then the method
in the subclass is said to override the method in
the superclass.
When an overridden method is called from
within its subclass, it will always refer to the
version of that method defined by the subclass.
The version of the method defined by the
superclass will be hidden.
Method Overriding
Method Overriding
Constructor Overloading
Constructor Overloading
Dynamic Method Dispatch
Method overriding forms the basis for one of
Java’s most powerful concepts: Dynamic
Method Dispatch.
Dynamic method dispatch is the mechanism
by which a call to an overridden method is
resolved at run time, rather than compile
time.
Dynamic method dispatch is important
because this is how Java implements run-
time polymorphism.
Dynamic Method Dispatch
Superclass reference variable can refer to a
subclass object.
When an overridden method is called
through a superclass reference, Java
determines which version of that method to
execute based upon the type of the object
being referred to at the time the call occurs.
It is the type of the object being referred to
that determines which version of an
overridden method will be executed.
Dynamic Method Dispatch
Dynamic Method Dispatch
Dynamic Method Dispatch
Dynamic Method Dispatch
Using Abstract Classes
There are situations in which you will want to
define a superclass that declares the structure
of a given abstraction without providing a
complete implementation of every method.
Create a superclass that only defines a
generalized form that will be shared by all of its
subclasses, leaving it to each subclass to fill in
the details.
Such a class determines the nature of the
methods that the subclasses must implement.
Using Abstract Classes
Any class that contains one or more abstract
methods must also be declared abstract.
To declare a class abstract, you simply use
the abstract keyword in front of the class
keyword.
There can be no objects of an abstract
class. That is, an abstract class cannot be
directly instantiated with the new operator.
Any subclass of an abstract class must either
implement all of the abstract methods in the
superclass, or be declared abstract itself.
Using Abstract Classes
Using Abstract Classes
Abstract classes cannot be used to
instantiate objects.
But, they can be used to create object
references.
Possible to create a reference to an abstract
class so that it can be used to point to a
subclass object.
Using Abstract Classes
Using Abstract Classes
Using Abstract Classes
Using Abstract Classes
Using final with Inheritance
Using final to Prevent Overriding:
To disallow a method from being overridden,
specify final as a modifier at the start of its
declaration. Methods declared as final cannot
be overridden.
Using final with Inheritance
Using final to Prevent Inheritance
Sometimes you will want to prevent a class from
being inherited.
To do this, precede the class declaration with
final.
Declaring a class as final implicitly declares all of
its methods as final, too.
Thank You