Chapter 1
Chapter 1
Books
The Complete Reference Java 2 (10th Edition)
Hebert Schildt, Tata Mc Graw Hill
Object-Oriented Programming with C++ and Java
Debasis Samanta, Prentice Hall of India
Website
https://cse.iitkgp.ac.in/~dsamanta/java/index.htm
Concept of Java Programming
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.
• The team wanted something that reflected the essence of the technology:
revolutionary, dynamic, lively, cool, unique, and easy to spell and fun to say.
• 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
Current popularity
Java has Platform
Language Score worldwide Independence
popularity
Java offers
Parallel and
Distributed
backward
development compatibility
Assembler
Interpreter
Compiler
DEBASIS SAMANTA
CSE
IIT KHARAGPUR
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.
High-level Programming Principles
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 the
Data Access Object uses local data and can be accessed in a control manner
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 No. Address
Cost Marks
Borrower DOI Books[]
Public
Issues()
Book Returns()
Protected
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) : Image + Document Paste an Image to a document
Add(Img, Doc)
Add(Doc1, Doc2) Add(Doc1, Doc2) : Document1 + Document2 Merge two documents
Java Programming Features
Features of Java programming
Interfaces Multithreading Java multimedia Java script
class HelloWorldApp
int main()
{
{
public static void main(String args[]){
printf("Hello, World!");
System.out.println("Hello, World!");
return 0;
}
}
}
– Move to the directory where your Java program has been saved
javac HelloWorldApp.java
Java program compilation
Java program compilation
Java program execution
To execute the Java program, type the command java (from the
command prompt).
java HelloWorldApp
Java program execution
Java program execution
C/C++ versus Java execution
C
JAVA
C++ versus 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
Questions to think…
– Contains the basic tools and libraries necessary for creating, testing,
documenting and executing Java programs.
Free Java Download : Download Java for your desktop computer now
https://www.java.com/en/download/index.jsp
Resource for Java programming
• There are many resources for learning Java
The JavaTM2 Tutorials
• The Java tutorials are practical guides for programmers who want to use the Java
programming language to create applications.
https://java.sun.com/docs/books/tutorial/index.html
• java.util – it contains miscellaneous classes like Vector, Stack, List, Date, Dictionary,
Hash etc.
• Mojo from Penumbra Software (best visual environment for creating Java
applets)
NotePad++ - https://notepad-plus-plus.org/download/v7.5.8.html
In Java, every variable has a type declared in the source code. There are two
kinds of types: reference types and primitive types. Reference types are
references to objects. Primitive types directly contain values.
int a, b = 0;
a = 123;
b = 45;
int c = a + b;
marks
marks.length = n
Example:
int x [ ] = new int [100];
Storing elements in array
Initialization of Array
<arrayName> [<subscript> ] = <value>;
Example:
x [5] = 100;
Example:
int x [ ] = {12, 3, 9, 15};
Here, declaration, allocation of memory and array initialization all are at one go!
Processing elements in an array
• Insertion
• Insertion at any location
• Insertion at front
• Insertion at end
• Insertion is sorted order
• Deletion
• Deletion a particular element
• Deletion an element at a particular location
• Deletion the element at front
• Deletion the element at end
• Searching and Traversal
• Finding the smallest and largest element
• Printing all element or some specific element
• Sorting
• In ascending order, descending order, lexicographical order etc.
Array in Java: A quick visit
• Declaration of an array • Initialization of an array
Examples Examples
int numbers[ ];
int numbers[] = {5, 4, 2, 1, 3};
float averageScores[ ];
int [ ] rollNo; float marks[] = {2.5, 3.4, 4.5};
float [ ] marks;
int myArray [ ] [ ];
myArray = new int [3] [4];
OR
int myArray [ ] [ ] = new int [3] [4];
Loading a 2D array
Initializing a 2D array : An example
1 2 3
4 5 6
OR
int myArray [ ] [ ] = { {1, 2, 3}, {4, 5, 6} };
Variable sized 2D array
x[0]
x[1]
x[2]