SEMESTER III
1
CORE THEORY PAPER – 3
JAVA Programming
Syllabus
Objectives:
To improve Object Oriented Programming gathered already through an independent platform.
Unit – I: BASICS, ESSENTIALS, CONTROL STATEMENT AND CLASSES &
OBJECTS
Computer and its Languages – Stage, Origin and Features for Java - JDK–OOP; Java Essentials:
Program – API - Variables& Literals - Data Types - String Class – Operators - Type conversion -
Constants - Scope – Comments - Keyboard Input; Control Statements: Conditional Statements –
Looping Statements - Break and Continue Statements; Classes and Objects: Modifiers -
Arguments - Constructors - Packages and import - Static Class - Overloaded Methods and
Constructors - Returning Objects – to String() - this reference –Enumeration - Garbage
Collection.
Unit – II: ARRAYS, INHERITANCE, INTERFACES AND PACKAGES
Arrays - Three or More Dimensions; Inheritance: Basics - Calling the Super class Constructor -
Overriding Super class Methods - Inheritance from Subclasses – Polymorphism -Abstract
Classes and Methods - Interfaces: Fields - Multiple inheritance - Interface inheritance; Packages:
Creating packages – Accessing package from other packages- Access Specifier.
Unit – III: STRING HANDLING, EXCEPTION HANDLING AND MULTI THREADING
String Handling: Basics - Operations –String Methods - String Buffer class - String Builder – to
String method -String Tokenizer class. Exception Basics: try and catch block - Multiple catch
block - Nested try - throws keyword - Throw vs Throws - Final Vs Finally Vs Finalize - Method
Overriding - Custom Exception - Multithreading: Life Cycle - Methods in Thread - thread
application – Thread priority – Synchronization - Inter-thread communication - Suspending,
Resuming, and Stopping Threads;
Unit – IV: APPLET AND GUI APPLICATION
Applets: Basis - Lifecycle - Applet classes - Application – Graphics; AWT-I: GUI Programming
- AWT classes - Windows fundamentals- Creating Windows - Dialog Boxes - Layout Managers
- Radio Buttons and Check Boxes – Borders-Swing
Unit – V: JAVA DATABASE CONNECTIVITY
JDBC - Types of Drivers- Architecture- Classes and Interfaces - Developing JDBC Application -
New Database and Table with JDBC - Working with Database Metadata.
Text Book
1. S.Sagayaraj, R.Denis, P.Karthik & D.Gajalakshmi, “Java Programming“, Universities
Press, 2017
2
Unit – I
Introduction to Java?
Java is a high level, robust, secured, object-oriented programming language and platform
developed and by James Gosling and others at Sun Microsystems in 1991.
It is a combination of features of C and C++ with some essential additional concepts.
Java code that runs on one platform does not need to be recompiled to run on another
platform; it's called write once, run anywhere (WORA).
It was first introduced to the public in 1995 and is widely used to
create Internet applications and other software programs.
Today, Java (JDK) is maintained and owned by Oracle.
Originally name known as OAK (Object Application Kernel) or Greentalk.
Java can be used to create two types of programs.
Application Applets
An application is a program that runs on An Applet is a small window based
the user’s computers under the operating program that runs on HTML page using a
system java enabled web browser
like internet Explorer, Netscape
Navigator or an Applet Viewer.
.
Fig (a)
3
Java Program
Compiler (Byte code)
Java Virtual Machine (JVM) [Interpreter]
Execute Execute Execute
Linux OS Windows Mac OS
Fig (b)
Fig (a) and (b): Java Execution
There were five primary goals in the creation of the Java language:
1. It should use the object-oriented programming methodology.
2. It should allow the same program to be executed on multiple operating systems.
3. It should contain built-in support for using computer networks.
4. It should be designed to execute code from remote sources securely.
5. It should be easy to use by selecting what was considered the good parts of other object-
oriented languages.
4
Computer and its Languages:
1. Computer Systems
Computer can be defined as an electronic device for processing data that takes data input
from its user, stores, processes data and generates that required output as per the processing
instructions given to it by the user.
Figure: Computer (Laptop)
Program
The set of instructions is called program
It can be written using artificial languages like C, C++ and Java etc.
The programs run under the supervision of an operating system example Windows
UNIX, LINUX etc. Here is
C, C++, Java etc your
Tell Program
me
what
do to
1000101 10100110
0110011 11100001
1001101 00101110
Figure: Program
Computer System Operations
It performs the following three operations in sequence
5
1. It receives data 2. Processes the 3. Provides the
and instructions data as per result (Output) in a
from the input instructions desired form
device
Figure: Computer system operations
Computer System Elements
The computer system consists of two major elements
Hardware Software
Hardware
The physical components of a computer system are called hardware.
The devices are required for input, output, storage and processing of the data.
Monitor
Keyboard Printer
Motherboar Hardware
Processor
Hard disk Floppy disk drive
Figure: some of the hardware devices
A typical computer system consists of the following major components
Central Processing Unit (CPU)
Main memory (RAM)
6
Secondary storage device
Input devices
Output devices
Central Processing Unit (CPU)
A central processing unit (CPU) is the electronic circuitry within a computer that carries
out the instructions of a computer program by performing the basic arithmetic, logical, control
and input/output (I/O) operations specified by the instructions.
Main Memory (RAM-Primary Storage)
Random-access memory is a form of computer data storage that stores data and machine
code currently being used.
A random-access memory device allows data items to be read or written in almost the
same amount of time irrespective of the physical location of data inside the memory
Secondary storage device
A secondary storage device refers to any non-volatile storage device (never loss their
data when the Power goes switched off) that is internal or external to the computer.
It can be any storage device beyond the primary storage that enables permanent data
storage.
A secondary storage device is also known as an auxiliary storage device or external
storage.
Ex: Removable hard drives, DVDs etc
Input Devices
Stage:
By the end of the 1980s and the early 1990s, object-oriented programming using C++
took hold.
Indeed, for a brief moment it seemed as if programmers had finally found the perfect
language. Because C++ blended the high efficiency and stylistic elements of C with the
object-oriented paradigm, it was a language that could be used to create a wide range of
programs.
7
However, just as in the past, forces were brewing that would, once again, drive computer
language evolution forward. Within a few years, the World Wide Web and the Internet
would reach critical mass. This event would precipitate another revolution in
programming.
Origin:
Java is a general-purpose, object-oriented programming language developed by Sun
Microsystems of USA in 1991.
Originally called Oak by James Gosling, one of the inventors of the language, Java was
designed for the development of software for consumer electronic devices like TVs,
VCRs, toasters and such other electronic machines.
This goal had a strong impact on the development team to make the language simple,
portable and highly reliable.
The Java team which included Patrick Naughton discovered that the existing languages
like C and C+ + had limitations in terms of both reliability and portability.
However, they modeled their new language Java on C and C++ but removed a number of
features of C and C++ that were considered as sources of problems and thus made Java a
really simple, reliable, portable, and powerful language.
Below Table lists some important milestones in the development of Java.
Year Development
1990 Sun Microsystems decided to develop special software that could be used to
manipulate consumer electronic devices.
A team of Sun Micro systems programmers headed by James Gosling was formed to
undertake this task.
After exploring the possibility of using the most popular object-oriented language
1991
C+ +, the team announced a new language named "Oak".
The team, known as Green Project team by Sun, demonstrated the application of their
1992
new language to control a list of home appliances using a hand-held device with a
tiny touch sensitive screen.
The World Wide Web CNWWJ appeared on the Internet and transformed the text-
1993
based Internet into a graphical-rich environment.
The Green Project team came up with the idea of developing Web applets (tiny
programs) using the new language that could run on all types of computers connected
to Internet
The team developed a Web browser called "Hot Java" to locate and run applet
1994
programs on Internet.
Hot Java demonstrated the power of the new language, thus making it instantly
popular among the Internet users.
8
Oak was renamed "Java", due to some legal snags.
1995
Java is just a name and is not an acronym.
Many popular companies including Netscape and Microsoft announced their support
to Java.
Java established itself not only as a leader for Internet programming but also as a
1996
general purpose, object-oriented programming language.
Java found its home. Sun releases Java Development Kit 1.0
Sun releases Java Development Kit 1.1 (JDK 1.1)
1997
Sun releases Java 2 with version 1.2 of the Software Development Kit (SDK 1.2)
1998
Sun releases Java 2 Platform, Standard Edition (J2SE) and Enterprise Edition (J2EE)
1999
J2SE with JDK 1.3 was released.
2000
J2SE with JDK 1.4 was released.
2002
J2SE with JDK 5.0 (instead of JDK 1.5) was released. This is known as J2SE 5.0
2004
Java SE 6
2006
Java SE 7
2011
Java SE 8
2014
Java SE 9
2017
Table: - Java Milestone
The most striking feature of the language is that it is a platform-neutral language.
Java is' the first programming language that is not tied to any particular hardware or
operating system.
Programs developed in Java can be executed anywhere on any system.
We can call Java as a revolutionary technology because it has brought in a fundamental
shift in how we develop and use programs. Nothing like this has happened to the software
industry before.
9
(Oak is a symbol of strength and chosen as a national tree of many countries like U.S.A., France,
Germany, Romania etc.)
Features for Java:-
The inventors of Java wanted to design a language which could offer solutions to some of
the problems encountered in modern programming. They wanted the language to be not only
reliable, portable and distributed but also simple, compact and interactive.
Sun Microsystems officially describes Java with the following attributes:
Platform-Independent
and Portable
Compiled and Object-
Interpreted
Robust and Secure Distributed
Familiar, Simple and
Small High Performance
Multithreaded and Interactive Dynamic and Extensible
Although the above appears to be a list of buzzwords, they aptly describe the full
potential of the language. These features have made Java the first application language of the
World Wide Web. Java will also become the premier language for general purpose stand-alone
applications.
Compiled and Interpreted
Usually a computer language is either compiled or interpreted.
10
Java combines both these approaches thus making Java a two-stage system.
First, Java compiler translates source code into what is known as bytecode instructions.
Bytecodes are' not machine instructions and therefore, in the second stage, Java
interpreter generates machine code that can be directly executed by the machine that is
running the Java program.
We can thus say that Java is both a compiled and an interpreted language.
Fig: Compiled and Interpreted
Platform-Independent and Portable
Fig (a) Platform-Independent
Fig (b) Portable
The most significant contribution of Java over other languages is its portability.
11
Java programs can be easily moved from one computer system to another, anywhere and
anytime.
Changes and upgrades in operating systems, processors and system resources will not
force any changes in Java programs. This is the reason why Java has become a popular
language for programming on Internet which interconnects different kinds of systems
worldwide.
We can download a Java applet from a remote computer onto our local system via
Internet and execute it locally. This makes the, Internet an extension of the user's basic
system providing practically unlimited number of accessible applets and applications.
Java ensures portability in two ways. First, Java compiler generates bytecode instructions
that can be implemented on any machine. Secondly, the sizes of the primitive data types
are machine-independent '.
Object-Oriented
Java is a true object-oriented language. Almost everything in Java is an object. All
program code and data reside within objects and classes.
Java comes with an extensive set of classes, arranged in packages that we can use in our
programs by inheritance.
The object model in Java is simple and easy to extend.
Fig: Object = Data + Methods
Robust and Secure
Java is a robust language.
It provides many safeguards to ensure reliable code.
It has strict compile time and run time checking for data types.
It is designed as a garbage-collected language relieving the programmers virtually all
memory management problems.
12
Java also incorporates the concept of exception handling which captures series errors and
eliminates any risk of crashing the system.
Security becomes an important issue for a language that is used for programming on
Internet.
Threat of viruses and abuse of resources is everywhere.
Java systems not only verify all memory access but also ensure that no viruses are
communicated with an applet.
The absence of pointers in Java ensures that programs cannot gain access to memory
locations without proper authorization.
Fig: Secure
Distributed
Java is designed as a distributed language for creating applications on networks.
It has the ability to share both data and programs.
Java applications can open and access remote objects on Internet as easily as they can do
in a local system. This enables multiple programmers at multiple remote locations to
collaborate and work together on a single project.
13
Fig: Distributed
Simple, Small and Familiar
Java is a small and simple language.
Many features of C and C+ + that are either redundant or sources of unreliable code are
not part of Java. For example, Java does not use pointer, preprocessor header files, goto
statement and many others.
It also eliminates· operator overloading and multiple inheritance.
Familiarity is another striking feature of Java.
To make the language look familiar to the existing programmers, it was modeled on C
and C++ languages.
Java uses many constructs of C and C++ and therefore, Java code "looks like a C++"
code. In fact, Java is a simplified version of C++.
Fig: Simple, Small and Familiar
Multithreaded and Interactive
Multithreaded means handling multiple tasks simultaneously.
Java supports multithreaded programs. This means that we need not wait for the
application to finish one task before beginning another.
For example, we can listen to an audio clip while scrolling a page and at the same time
download an applet from a distant computer. This feature greatly improves the interactive
performance of graphical applications.
The Java runtime comes with tools that support multi process synchronization and
construct smoothly running interactive systems.
14
High Performance
Java is impressive for an interpreted language, mainly due to the use of intermediate
bytecode. According to Sun, Java speed is comparable to the native C/C++.
Java architecture is also designed to reduce overheads during runtime.
Further, the incorporation of multithreading enhances the overall execution speed of Java
programs.
Fig: High Performance
Java uses Just-in-Time compiler to enable performance.
Just-In-Time compiler is a program that turns Java bytecode into instructions that can be
sent directly to the processor
Dynamic and Extensible
Java is a dynamic language.
15
Java is capable of dynamically linking in new class libraries, methods, and objects.
Java can also determine the type of class through a query, making it possible to either
dynamically link or abort the program, depending on the response.
Java programs support functions written in other languages such as C and C+ +. These
functions are known as native methods. This facility enables the programmers to use the
efficient functions available in these languages. Native methods are linked dynamically at
runtime.
Fig: Dynamic and Extensible
JDK (Java Development Kit)
JRE
16
JRE is an acronym for Java Runtime Environment.
It is used to provide runtime environment.
It is the implementation of JVM. It physically exists.
It contains set of libraries + other files that JVM uses at runtime.
Implementations of JVMs are also actively released by other companies besides Sun
Micro Systems.
JVM
JVM (Java Virtual Machine) is an abstract machine.
It is a specification that provides runtime environment in which java bytecode can be
executed.
JVMs are available for many hardware and software platforms.
JVM, JRE and JDK are platform dependent because configuration of each OS differs.
But, Java is platform independent. There are three notions of the JVM: specification,
implementation, and instance.
The JVM performs following main tasks:
Loads code
Verifies code
Executes code
Provides runtime environment
JDK
17
JDK is an acronym for Java Development Kit. It physically exists. It contains JRE +
development tools.
The Java Development Kit (JDK) is a software development environment used for
developing Java applications and applets.
It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler
(javac), an archiver (jar), a documentation generator (Javadoc) and other tools needed in
Java development.
Java Development Kit is the official development kit for the Java programming language.
Java Development Kit contains the software and tools that you need to compile, debug, and run
applets and applications written using the Java programming language.
JDK has a collection of programming tools, including javac, jar, and the archiver, which
packages related class libraries into a single JAR file. This tool also helps manage JAR files,
javadoc - the documentation generator, which automatically generates documentation from
source code comments, jdb - the debugger, jps - the process status tool, which displays process
information for current Java processes, javap - the class file disassembler, and other components
are also included.
Key Contents include:
18
Development Tools (In the bin/ subdirectory) Tools and utilities that will help you
develop, execute, debug, and document programs written in the JavaTM programming
language.
Runtime Environment (In the jre/ subdirectory) An implementation of the Java Runtime
[
Environment (JRE) for use by the JDK. The JRE includes a Java Virtual Machine (JVM),
class libraries, and other files that support the execution of programs written in the Java
programming language.
Additional Libraries (In the lib/ subdirectory) Additional class libraries and support
files required by the development tools.
Demo Applets and Applications (In the demo/ subdirectory) Examples, with source
code, of programming for the Java platform. These include examples that use Swing and
other Java Foundation Classes, and the Java Platform Debugger Architecture.
Sample Code (In the sample subdirectory) Samples, with source code, of programming
for certain Java API's.
C header Files (In the include/ subdirectory) Header files that support native-code
programming using the Java Native Interface, the JVM Tool Interface, and other
functionality of the Java platform.
Source Code (In src.zip) Java programming language source files for all classes that
make up the Java core API (that is, sources files for the java.*, javax.* and some org.*
packages, but not for com.sun.* packages). This source code is provided for
informational purposes only, to help developers learn and use the Java programming
language. These files do not include platform-specific implementation code and cannot
be used to rebuild the class libraries. To extract these file, use any common zip utility. Or,
you may use the Jar utility in the JDK's bin/ directory: jar xvf src.zip.
The JDK also comes with a complete Java Runtime Environment. This consists of a Java
Virtual Machine and all of the class libraries present in the production environment, as well as
additional libraries, internationalization libraries and the IDL libraries.
19
Fig: JDK
20
OOP's Concept in Java
Object-Oriented Programming is a methodology or paradigm to design a program using
classes and objects.
Object
21
Any entity that has state and behavior is known as an object. For example: chair, pen,
table, keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class.
It is a logical entity.
Classes are user-defined data types and behave like the built-in types of a programming
language.
For example, the syntax used to create an object is no different than the syntax used to
create an integer object in C.
If fruit has been defined as a class, then the statement
Fruit
mango;
Will create an object mango belonging to the class fruit
Inheritance
When one object acquires all the properties and behaviours of parent object i.e.
known as inheritance. It provides code reusability.
It is used to achieve runtime polymorphism.
Inheritance is the process by which objects of one class acquire the properties of objects
of another class. Inheritance supports the concept of hierarchical classification.
In OOP, the concept of inheritance provides the idea of reusability: This means that we
can add additional features to an existing class without modifying it. This is possible by
deriving a new class from the existing one.
The new class will have the combined features of both the classes. Thus the real appeal
and power of the inheritance mechanism is that it allows the programmer to reuse a class
that is almost, but not exactly, what he wants, and to tailor the class in such a way that it
does not introduce any undesirable side effects into the rest of the classes.
In Java, the derived class is known as 'subclass'.
22
Polymorphism
When one task is performed by different ways i.e. known as polymorphism.
For example: to convince the customer differently, to draw something e.g. shape or
rectangle etc.
In java, we use method overloading and method overriding to achieve polymorphism
Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.
Polymorphism means the ability to take more than one form. For example, an operation
may exhibit different behaviour in different instances. The behaviour depends upon the
types of data used in the operation
23
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 class and interface to achieve abstraction.
Abstraction refers to the act of representing essential features without including the
background details or explanations.
Classes use the concept of abstraction and are defined as a list of abstract attributes such
as size, weight and cost, and methods that operate on these attributes. They encapsulate
all the essential properties of the objects that are to be created.
24
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as
encapsulation.
For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
.
Data encapsulation is the most striking feature of a class.
The data is not accessible to' the outside world and only those methods, which are
wrapped in the class, can access it.
These methods provide the interface between the object's data and the program. This
insulation of the data from direct access by 'the program is called data hiding.
Encapsulation makes it possible for objects to be treated like 'black boxes', each
performing a specific task without any concern for internal implementation.
25
26