16CS6402
INTRODUCTION TO JAVA
PROGRAMMING
Text Book
T1- Herbert Schildt, ―Java The complete reference‖, 8th Edition, McGraw Hill
Education, 2011.
T2 - Cay S. Horstmann, Gary cornell, ―Core Java Volume –I Fundamentals‖, 9th
Edition, Prentice Hall, 2013.
UNITS
• UNIT I - INTRODUCTION TO JAVA FUNDAMENTALS
• UNIT II - INTRODUCTION TO OOP AND INHERITANCE
• UNIT III – INHERITANCE AND INTERFACES
• UNIT IV – EXCEPTION HANDLING AND I/O
• UNIT V – MULTITHREADING AND GENERIC
PROGRAMMING
UNIT I - INTRODUCTION TO JAVA FUNDAMENTALS
•Introduction to java programming
• Features of Java Language,
• JVM -The Java Environment
•Fundamental Programming Structures in Java
• Comments
•Primitive Data types •Packages
•Variables • -defining package
•operators • -access protection
•Arrays • -importing packages
•Control statements • JavaDoc comments.
Review of Object Oriented
Programming
1. Review of Object Oriented Programming:
Object-oriented programming (OOP) is the core of Java.
OOP allows decomposition of a problem into a number of entities called
objects and then builds data and function around these objects.
The data of an object can be accessed only by the function associated with
that object.
However, function of one object can access the function of other objects.
Basic Concepts of Object Oriented Programming:
Basic Concepts of Object Oriented Programming:
The basic Concepts used extensively in Object Oriented
Programming are,
Objects
Classes
Data abstraction and encapsulation
Inheritance
Polymorphism
Dynamic binding
Message passing
1.Objects :
An object is an instance of a class.
Objects are variables of the type class and it is an real time
entity. Every object is associated with data and functions
which define meaningful.
An Object is a collection of data members and associated
member functions.
2. Class
•Class is a collection of Objects.
•Class is a collection of data members and member
functions.
•Objects with similar properties and methods are
grouped together to form a Class.
•An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is
allocated.
Example:
Example:
Car Santro
Car Alto
Car Etios
Class Object
Class : Car
Attributes:
Name
Model //data members
Year of Manufacture
Colour
Engine Power
Operations:
Start( ) // member functions
Stop( )
Accelerate( )
Class and Objects
3.Data Abstraction:
Data Abstraction represents only the essential
features and hides the implementation details.
Data Abstraction increases the power of
programming language by creating user defined data
types called Abstract Data Types (ADTs).
4.Data Encapsulation:
The wrapping up of data and functions into a single unit
called class is known as Encapsulation.
Data Encapsulation enables the important concept of data
hiding or information hiding.
5. Inheritance:
•Inheritance is the process of deriving the properties (data
members and member functions) of one class (base class) to
the another class (derived class).
•The base class is also known as parent class or super class,
the new class that is formed will have the combined features
of both the classes and is called as derived class.
•Derived class is also known as a child class or sub class.
Example:Inheritance
Bird
Flying Bird Non - Flying Bird
Parrot Crow Penguin Kiwi
6. Polymorphism:
Polymorphism means ability to take many forms.
It allows a single name/operator to be associated with
different operations depending on the type of data passed to
it. Examples of polymorphism are function overloading,
operator overloading and dynamic binding (virtual
functions).
Polymorphism:Example
Shape
Draw ()
Circle object Box Object Triangle Object
Draw (circle) Draw (box) Draw (triangle)
7. Dynamic Binding
Binding refers to the linking of a procedure call to the code
to be executed in response to the call.
Dynamic binding or late binding means that the code
associated with a given procedure call is not known until the
time of the call at run time.
8. Message Passing
OOPs consist of a set of objects that communicate with
each other. Message passing involves following steps-
1. Creating classes that define objects and their behavior
2. Creating objects from class definitions and
3. Establishing communication among objects through
message passing.
Example:
customer. balance(account no)
object message information
BENEFITS OF OOPs
OOP offers several benefits to both the program designer and the user. The
principal advantages are.
Through inheritance, we can eliminate redundant code and extend the use
of existing classes
We can build program from the standard working module that
communicate with one another, rather than having to start writing the code
from scratch. This leads to saving of development time and higher
productivity.
The principal of data hiding helps the programmer to build secure
programs.
It is possible to have multiple instance of an object to co-exist without any
interference
It is easy to partition the work in a project, based on objects.
Object oriented systems can be easily upgraded from small to large
systems.
Message passing techniques for communication between objects makes the
interface description with external systems much simpler.
Software complexity can be easily managed.
HISTORY OF
JAVA
FATHER OF
JAVA
James Gasoline 3
“ In 1991, “Green Team” of Sun
Microsystem
leded by James Gosling
developed the Java
Programming Language.
Previously it was called Oak, later
in 1995, it was named Java.
4
▰ Java is also regarded as
Internet programming language.
It can make web pages more
dynamic, responsive using Java
applet whereas HTML is mostly
flat & static.
▰ Java typically offers three
editions: SE (Client-side apps.),
EE (Server-side apps.) and
ME (apps. for mobile devices)
5
HISTORY OF JAVA
▰ 1990 oak : To control microprocessors
Embedded in customer item
▰ Oak needed to be
– Platform independent
– Extremely reliable
– Compact
▰ 1993: Java
– Internet and web exploration
– Internet application
▰ 1994: Hot Java Browser
▰ 1995: java1. 0 6
VERSIONS OF
JAVA
▰ 1995 version 1.0:
– The Java development kit was released for free by the sun
– 8-packages 212-classes
– Microsoft and other companies licensed Java
▰ 1997 version 1.1:
– 23 packages, 504 classes
– Improvement include better event handling inner classes , improved JVM
▰ 1999 version 1.2:
- It is also called as the Java 2 platform
- 59 packages 1520 classes
7
- Collection API included list sets and hash map
VERSIONS OF
JAVA
▰ 2000 VERSION 1.3:
- 76 packages 1842 classes
- Java sound
▰ 2002 VERSION 1.4:
- 135 PACKAGES 2991 classes
- Improved I , xml support etc..,
▰ 2004 VERSION 5.0 (1.5):
- 165 packages over 3000 classes
- Faster startup metadata formatted output
▰ 2006 JAVA SE 6:
– Scripting language support 8
VERSIONS OF
JAVA
▰ 2011 JAVA SE 7:
– JVM support for dynamic language
– String in switch
– Allowing underscores in numeric literals
▰JAVA SE 8:
– LAMBDA OPERATOR is expected in summer 2013
JAVA SE #:
JAVA SE #:
.
.
. Java 21, the latest, was released on September 19, 2023.
9
TOPIC 1
•Introduction to java programming
•Features of Java Language,
•JVM -The Java Environment
UNIT I - Features of Java (Java buzz words):
1. Simple
According to Sun, Java language is simple because:
•syntax is based on C++ .
•removed many confusing and/or rarely-used features e.g., explicit pointers,
operator overloading etc.
•No need to remove unreferenced objects because there is Automatic Garbage
Collection in java.
2. Object-oriented
•Like C++, java uses all the OOP concepts like encapsulation, inheritance and
polymorphism etc.
•Java is called a purely object oriented language as everything we write inside
class
in a java program.
Features of Java (Java buzz
words)(Contn…)
3.Architecture-Neutral
Java code can be run on multiple platforms e.g. Windows, Linux, Sun
Solaris, Mac/OS etc.
Java code is compiled by the compiler and converted into bytecode. This
bytecode is a platform-independent code because it can run on any
machine with any processor and with any OS.
Write Once and Run Anywhere(WORA).
Features of Java (Java buzz
words)(Contn…)
4. Security
When we use a Java-compatible Web browser, we can safely download Java
applets without fear of viral infection or malicious intent.
Java achieves this protection by confining a Java program to the Java
execution environment and not allowing it access to other parts of the
computer. This control is exercised by JVM.
5. Robust (Strong/ Powerful):
Java programs will not crash because of its exception handling and its
memory management features.
Java uses strong memory management. There is a lack of pointers that avoids
security problem.
There is automatic garbage collection in java.
There is exception handling and type checking mechanism in java. All these
points make java robust.
Features of Java (Java buzz words)
(Contn…)
Portability:
Java does not have implementation dependent aspects and it yields or gives
same result on any machine.
We may carry the java bytecode to any platform.
Distributed:
Java is designed for use on network; it has an extensive library which
works in agreement with Transmission Control Protocol (TCP) and the
Internet Protocol (IP) .
We can create distributed applications in java.
RMI (Remote Method Invocation) and EJB (Enterprise JavaBeans) are
used for creating distributed applications.
We may access files by calling the methods from any machine on the
internet.
Features of Java (Java buzz
words)(Contn…)
Multithreaded
Java was designed to meet the real-world requirement of creating interactive,
networked programs.
Java supports multithreaded programming, which allows us to write programs
that do many things simultaneously.
The Java run-time system comes with an elegant yet sophisticated solution for
multiprocess synchronization that enables us to construct smoothly running
interactive systems.
Dynamic
Java programs carry with them substantial amounts of run-time type
information that is used to verify and resolve accesses to objects at run time.
This makes it possible to dynamically link code in a safe and expedient
manner.
This is crucial to the robustness of the applet environment, in which small
fragments of byte code may be dynamically updated on a running system.
JVM
JVM i.e., Java Virtual Machine.
JVM is the engine that drives the Java code.
Mostly in other Programming Languages, compiler produce code
for a particular system but Java compiler produce Bytecode for a
Java Virtual Machine.
When we compile a Java program, then bytecode is generated.
Bytecode is the source code that can be used to run on any platform.
Bytecode is an intermediary language between Java source and the
host system.
It is the medium which compiles Java code to bytecode which gets
interpreted on a different machine and hence it makes it
Platform/Operating system independent.
JVM
JVM generates a .class(Bytecode) file, and that file can be run in any OS,
but JVM should have in OS because JVM is platform dependent.
The Java Environment
Java includes many development tools, classes and
methods
◦ Development tools are part of Java Development Kit (JDK) and
◦ The classes and methods are part of Java Standard Library (JSL),
also known as Application Programming Interface (API).
JDK constitutes of Java Development tools like java
compiler, java interpreter (JVM) and many.
API includes hundreds of classes and methods grouped
into several packages according to their functionality.
STRUCTURE OF JAVA
PROGRAMMING
The Basic Structure of a Simple Java program
A Java program consists of different sections. Some of them are mandatory
but some are optional. The optional section can be excluded from the
program depending upon the requirements of the programmer.
STRUCTURE OF JAVA PROGRAMMING
(Cont…)
Documentation Section
It includes the comments that improve the readability of the program. A
comment is a non-executable statement that helps to read and understand
a program especially when your programs get more complex.
Eg:1 // Calculate sum of two numbers
Eg:2 /*calculate sum of two numbers
and it is a multiline comment */
Package Statement
A package is a collection of classes, interfaces and sub-packages.
A sub package contains collection of classes, interfaces and sub-
sub packages etc.
java.lang.*; package is imported by default and this package is
known as default package. It must appear as the first statement in
the source code file before any class or interface declaration. This
statement is optional.
STRUCTURE OF JAVA
PROGRAMMING (Cont…)
Import statements
An import statement is used for referring classes that are declared in other
packages. The import statement is written after a package statement but
before any class definition. You can import a specific class or all the
classes of the package.
Example If you want to import Date class of java.util package using
import statement then write.
import java.util.Date;
Interface Section
In the interface section, we specify the interfaces. An interface is similar to
a class but contains only constants and method declarations.
Class Definition:
Java program may contain multiple class definition. Classes are primary
feature of Java program. The classes are used to map real world problems.
STRUCTURE OF JAVA
PROGRAMMING (Cont…)
Main Method Class Section:
Every program in Java consists of at least one class, the one that contains the
main method. The main () method which is from where the execution of program
actually starts and follow the statements in the order specified. The class section
is mandatory.
First Java Program: print a message “Welcome to Java World”
class Example
{
public static void main (String args[])
{
System.out.println ("Welcome to Java World");
}
}
Output:
Welcome to Java World
How to compile the above program:
To compile the Example program, execute the compiler, javac, specifying the name
of the source file on the command line, as shown here:
C:\>javac Example.java
The javac compiler creates a file called Example.class that contains the byte code
version of the program. As discussed earlier, the Java byte code is the intermediate
representation of our program that contains instructions the Java interpreter will
execute. Thus, the output of javac is not code that can be directly executed. To
actually run the program, we must use the Java interpreter, called java. To do so,
pass the class name Example as a command-line argument, as shown here:
C:\>java Example
When the program is run, the following output is displayed:
Welcome to Java World
Total Platform
Independence
JAVA COMPILER
(translator)
JAVA BYTE CODE
(same for all platforms)
JAVA INTERPRETER
(one for each different system)
Windows 95 Macintosh Solaris Windows NT
Thank You