1.1 Java Unit1 Introduction
1.1 Java Unit1 Introduction
(BCS403)
by
Dr. Surendra Kr. Keshari
Associate Professor
KIET Group of Institutions
1
Introduction to JAVA
• Java is a programming language and a platform.
• 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.
• Java is a high level, robust, secured and object-oriented programming
language.
History of Java
• James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language
project in June 1991. The small team of Sun engineers called Green Team.
• Firstly, it was called "Greentalk" by James Gosling, and file extension was .gt.
• Java was originally designed for small, embedded systems in electronic appliances
like set-top boxes, but it was too advanced technology for the digital cable
television industry at the time.
• After that, it was called Oak and was developed as a part of the Green project. Java
team members initiated this project to develop a language for digital devices.
• In 1995, Time magazine called Java one of the Ten Best Products of 1995.
Simple Robust
Object-
Portable
Oriented
Secure
Java Architecture
Neutral
High
Dynamic
Performance
Platform
Multithreaded
Independent
Interpreted
Java Language Features
1) simple
2) object-oriented
3) robust
4) multithreaded
5) architecture-neutral
6) interpreted and high-performance
7) distributed
8) dynamic
9) secure
Java Language Features 1
1) simple – Java is designed to be easy for the professional programmer to
learn and use.
2) object-oriented – a clean, usable, pragmatic approach to objects, not
restricted by the need for compatibility with other languages.
3) robust – restricts the programmer to find the mistakes early, performs
compile-time (strong typing) and run-time (exception-handling) checks,
manages memory automatically.
Java Language Features 2
4) multithreaded – supports multi-threaded programming for writing
program that perform concurrent computations
5) architecture-neutral – Java Virtual Machine provides a platform-
independent environment for the execution of Java bytecode
6) interpreted and high-performance – Java programs are compiled into an
intermediate representation – bytecode:
a) can be later interpreted by any JVM
b) can be also translated into the native machine code for efficiency.
Java Language Features 3
7) distributed – Java handles TCP/IP protocols, accessing a resource
through its URL much like accessing a local file.
8) dynamic – substantial amounts of run-time type information to verify and
resolve access to objects at run-time.
9) secure – programs are confined to the Java execution environment and
cannot access other parts of the computer.
Garbage collection
➢Garbage collection is the mechanism that De allocate the memory of
an object when it is no longer referenced by a variable.
➢In c++, the memory of an object is released by use of delete
operator.
➢Java virtual machine determines when to free up the memory of an
object.
Programming languages
Program
Assembler
Interpreter
Compiler
Third generation programming languages
• A third generation (programming) language (3GL) is a grouping of programming languages that introduced significant enhancements to second
generation languages, primarily intended to make the programming language more programmer-friendly.
• English words are used to denote variables, programming structures and commands, and Structured Programming is supported by most 3GLs.
• Commonly known 3GLs are FORTRAN, BASIC, Pascal, JAVA and the C-family (C, C+, C++, C#, Objective-C) of languages. Also known as a
high-level programming language.
Function-oriented programming
Object-oriented programming
FOP versus OOP
Function Oriented Programming (FOP) Object Oriented Programming (OOP)
Program organization Program is divided into small parts called functions. Program is divided into parts called objects.
Importance Importance is not given to data but to functions Importance is given to the data rather than procedures
Approach FOP follows top down approach OOP follows bottom up approach
Access Specifiers Does not have any access specifier Has three access specifiers, namely Public, Private, Protected
Data Moving Data can move freely from function to function in the system Objects can move and communicate with each other
Maintainability To add new data and function is not so easy Provides an easy way to add new data and function
Function uses global data for sharing that can be accessed freely from function to function in
Data Access Object uses local data and can be accessed in a control manner
the system.
Data Hiding No data hiding is possible, hence security is not possible Provides data hiding, hence secured programming is possible
•Encapsulation
• Inheritance
• Information hiding
• Polymorphism
Encapsulation in Java
Title Name
data data
Author Roll No.
Accession Address
No. Marks
Cost Books[]
Borrower
Issue()
DOI Search Books()
Fine() Request()
Book Return() Borrower
Renew()
Open() Enroll()
methods methods
Close() Exit()
Encapsulation: Example
Books Borrowers
Inheritance in Java
Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object. It is an important
part of OOPs (Object-Oriented Programming system).
Inheritance: Example
DOP
Version
Title Department
Author
Accession Add() Remove()
No. Text
Cost
Borrower Publisher
DOI Rack No.
Permission
Book Issue() Copy Type
Fine()
Return() Display()
Open() Close()
Close() Reference Open()
Copy()
Information hiding
Public
Title Author
Protected
Account No.
Private
Cost
Public
Issues() Returns()
Protected
Book Resave()
Private
Open() Close()
Polymorphism
In object-oriented programming,
Image Document
polymorphism refers to a
programming language's ability to
process objects depending on their
class.
print()
Polymorphism: Example
print()
x, y;
s1, s2; Add(x, y) : 12 +34 Add two numbers
Img,Doc,Doc1,Doc2
Add(s1 + s2) : Debasis + Samanta Merge two strings
Add(x, y) Add(s1, s2)
Add(Img, Doc) Add(Img, Doc) : Image + Document Paste an Image to a document
Add(Doc1, Doc2)
Add(Doc1, Doc2) : Document1 + Document2 Merge two documents
Features of Java programming
Interfaces Multithreading Java multimedia Java script
Distributed
Packages programming
class HelloWorldApp
int main()
{
{
public static void main(String args[]){
printf("Hello, World!");
System.out.println("Hello, World!");
return 0;
}
}
}
JAVA
C++ versus Java
Areas of applications
–C++ is best suitable for developing large software.
• Library management system, Employee management system,
Passenger reservation system, etc.
Java
compiler
Java BYTE
code JVM
C++
Complier Java Java Java
interprter interprter interprter
C++ source C++ object WIN32 Solaris Macintosh
code code
C++ provides platform dependent programming Java provides platform independent programming
Tools available for Java programming
– Contains the basic tools and libraries necessary for creating, testing,
documenting and executing Java programs.
• java.awt – the Abstract Windowing Toolkit for designing GUI like Button, Checkbox,
Choice, Menu, Pannel, etc.
• java.lang – provides useful classes like to handle Object, Thread, Exception, String,
System, Math, Float, Integer, etc.
Packages in Java
• java.lang – provides useful classes like to handle Object, Thread, Exception, String,
System, Math, Float, Integer etc.
• java.util – it contains miscellaneous classes like Vector, Stack, List, Date, Dictionary,
Hash etc.
class MyProgram {
class Example{
public static void main(String args[]){ Calculator a = new Calculator();
a.i = 20;
System.out.println("Square root of "+a.i+" is "+a.x);
}
}
Let us examine each statement step-by-step.
Import
import java.lang.*;
Statements
class Calculator{ double i;
double x = Math.sqrt(i);
Declaration }
of class class Example{
public static void main(String args[]){ Calculator a = new
Calculator();
a.i = 20;
System.out.println("Square root of "+a.i+" is "+a.x);
Declaration of }
main class }
➢ Java program starts its execution from a method belongs to a class
only.
➢ The main() method is the starting point of the execution of the main thread.
➢ The name of the Java program should be named after this class so that Java
interpreter unanimously choose that class to start its execution.
Java main() method
➢ It is an access specifier, which allows the programmer to control the visibility of class members.
➢ public member may be accessed by code outside the class in which it is declared.
• This is necessary since main() is called by Java interpreter before any objects are made.
• The println(“…") method prints the string "..." and moves the cursor to a new line.
• The print("...") method instead prints just the string "...", but does not move the cursor to a
new line. Hence, subsequent printing instructions will print on the same line.
Note: The println() method can also be used without parameters, to position the cursor on the next
line.
Let us run this Java program