JAVA Programming
INTRODUCTION
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 Oak and was developed as a part of the green project, Java team
members initiated this project to develop a language for digital services.
Later, Java technology was incorporated by Netscape and a suited for networking.
Java was called Oak as it is a symbol of strength and chosen as a national tree of many
countries like USA, France, Germany, Romania etc..,
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, Oak was renamed as Java
Java is an island of Indonesia where first coffee was produced (called java coffee).
In 1995, Time magazine called Java one of the Ten Best Product of 1995.
JDK (Java Development Kit) 1.0 released in January 23, 1996.
Page 1 of 30
Current Polarity
How is Java Unique?
Von Neumann architecture of computing
Page 2 of 30
Programming Languages
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, Panel, JAVA and the C-Family (C,
C+, C++, C#, Objective – C) of languages. Also known as a highly-level programming
language.
High Level Programing Principles
Function – Oriented Programming
Page 3 of 30
Object – Oriented Programming
FOP versus OOP
JAVA Programming Paradigms
Java is based on the concept of object-oriented programming. As the name suggests,
at the center of it all is an object. Objects contain both data and the functionality
that operates on that data. This is controlled by the following four paradigms.
- Encapsulation
- Inheritance
- Information hiding
- Polymorphism
Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together into a single
unit, for example, a capsule which is mixed of several medicines.
Page 4 of 30
Example:
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).
Example
Page 5 of 30
Information hiding
Polymorphism
In Object – Oriented Programming, Polymorphism refers to a programming
language’s ability to process objects depending on their class.
Page 6 of 30
Features of Java Programming
JDBC – Java Deta Base Connectivity
JSP – Java Server Pages
Questions:
o Can a software be developed in Java so that it runs in any OS? Any Machine?
o How a browser (e.g., Mozilla, Google Chrome, Safari etc.) works in your mobile/
Computer?
JAVA PROGRAMMING STEPS
A Program in C to display message
A Program in JAVA to display message
Note: Both Java and C languages are case sensitive.
Page 7 of 30
C Verses Java
JAVA Program editing:
Any text editor can be used to write Java programs. For example
- In Windows
- Notepad, edit, Wordpad, MS-Word etc..
- In Unix
- vi, emacs, gedit etc.
Save the program
- Save the program in a file with the name
[Link]
Note: Name of the Program should be same as Name of the class
The Java compiler (javac) converts a Java program into Java byte code
- Open a DOS shell (in Windows) or terminal (in Unix)
- Move to the directory where your Java program has been saved
Page 8 of 30
- Enter the following command to compile:
javac [Link]
- Once the java program successfully compiled this java file converted into
different extension (i.e. [Link]).
- To execute the Java program, type the command java (from the command
prompt)
- For example, the current program [Link] can be
executed as
Java HelloWorldApp
Java Program Execution
Note
Java is a platform independent because of the platform .class. it can compile any
platform
C is a platform dependent. It is different is each platform
Page 9 of 30
C/C++ versus Java execution
C versus Java
C++ versus Java
Area of Applications
- C++ is best suitable for developing large software.
o Library management system, Employee management system, Passenger
reservation system, etc.
- Java is best suitable for developing communication/ Internet application software.
o Network prorocol, Internet programs, web page, web browser.
Page 10 of 30
JAVA TOOLS AND RESOURCES
Java Programming Tools:
Java Software Developer’s Kit (SDK):
- SDK from JavaSoft, a division of Sun Microsystems Inc.,
- Contains the basic tools and libraries necessary for creating, testing, documenting
and executing Java programs.
Java 2 SDK, Standard Edition
Page 11 of 30
Page 12 of 30
Page 13 of 30
JAVA Lanaguage Subset
Page 14 of 30
Identifiers in Java
Names give to various program elements (variables, constants, class, methods,
etc..,)
- May consists of letters, digit and the underscore(‘_’) character, with
no space between.
- Blank and comma are not allowed.
- First character must be an alphabet or underscore.
- An identifier can be arbitrary long.
- Identifier should not be a reserved word.
Java Programming language is case sensitive
Area, AREA and Area are all different
Page 15 of 30
Page 16 of 30
Page 17 of 30
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.,
Page 18 of 30
Page 19 of 30
Page 20 of 30
DEMONSTRATION - I
Steps to install java software and create path using environmental variable.
Steps to run the java program
First Java Program:
Program
class FirstJavaProgram {
Public static void main(String() args) {
[Link](“Congratulations! Your First Java Program Run Successfully”);
}
}
Output
Congratulations! Your First Java Program Run Successfully
Note: class name and program file name must be same
Program
class helloWorld {
Public static void main(String() args) {
[Link](“Hello ! World”);
[Link](“Hi…..”);
}
}
Output
Hello ! World
Hi….
Array Initializing Program:
Page 21 of 30
Program
class TestArray {
public static void main(String args()) {
int a() ={10, 20, 30, 40, 50}; //Initialization
//Traversing Array
for{int i=0; i<[Link]; i++) { //length is the property
[Link](a[i]);
}
// Average calculation
float sum = 0, avg;
for (int i = 0; i<[Link]; i++) // Calculating the sum of the all elements
sum += a[i];
avg = sum/[Link];
[Link](“Average = “ + avg);
}
}
Output
10
20
30
40
50
Average = 30.0
3D Array
Program
class a3DArray {
public static void main(String args[]) {
int my3DArray [ ] [ ] [ ] = new int [3] [4] [5];
int i, j, k;
for (i=0; i<3; i++)
for (j=0; j<4; j++)
for (k=0; k<5; k++)
my3DArray[i][j][k] = I * j * k;
for (i=0; i<3; i++) {
for (j=0; j<4; j++) {
for (k=0; k<5; k++)
[Link](my3DArray[i][j][k] + “ ”);
[Link]();
}
[Link]();
}
}
}
Output:
Page 22 of 30
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
JAVA APPLET PROGRAMMING
Java Programs are available in two flavors
Applet
A Java appley is a program that appears embedded in a web document and applet
come into effect when the browser browser the web page.
Applications
It is similar to all other kind of programs like in C, C++, etc. to solve a problem
Page 23 of 30
First JAVA APPLET program
Program
// An applet to print Hello World
import [Link];
import [Link];
public class HelloWorld extends Applet {
public void paint (Graphics g) {
[Link](“Hello World!”. 150, 150);
}
}
Output:
Page 24 of 30
Page 25 of 30
Structure of an Applet
Page 26 of 30
Page 27 of 30
Page 28 of 30
Page 29 of 30
Page 30 of 30