(c) Rajkumar
Java
Technology
1
Different Programming Paradigms
(c) Rajkumar
Functional/procedural programming:
program is a list of instructions to the computer
Object-oriented programming
program is composed of a collection objects
that communicate with each other
2
Java History
(c) Rajkumar Java is a general-purpose, object-oriented
programming language developed by Sun
Microsystems of USA in 1991 and was officially
launched in 1995.
Originally called Oak by Engineer James Gosling
invented for the development of software for
consumer electronic devices like TVs, toasters,
etc.
Java Authors: James, Arthur Van, and others.
3
Java
(c) Rajkumar
Java is a high-level, third generation programming
language like C, FORTRAN, Smalltalk and many
others.
Can be used to write computer applications that play
games, store data or do any of the thousands of
other things computer software can do.
Has its own structure, syntax, rules ands program
paradigm
Its programming paradigm is based on the concept
of OOP
4
Java applications
(c) Rajkumar
simple text-based programs called console
applications - these programs just support text input
and output to your computer screen.
graphic user interface (GUI, pronounced gooey)
applications - applications with menus, toolbars,
buttons, scroll bars, and other controls which
depend on the computer mouse for input.
Java applets
5
Benefits of Programming in Java
(c) Rajkumar
Get started quickly
Although the Java programming language is a
powerful object-oriented language, it's easy to learn,
especially for programmers already familiar with C
or C++.
Write less code
Comparisons of program metrics suggest that a
program written in the Java programming language
can be four times smaller than the same program in
C++.
6
Benefits of Programming in Java
(c) Rajkumar
Write better code
The Java programming language encourages
good coding practices.
Garbage collection helps you avoid memory
leaks.
Its object orientation and wide-ranging, easily
extendible API let you reuse other people's tested
code and introduce fewer bugs.
7
Benefits of Programming in Java
(c) Rajkumar
Develop programs faster
Development time may be as much as twice as
fast compared to writing the same program in
C++ because you write fewer lines of code
and it is a simpler programming language than
C++.
8
Benefits of Programming in Java
(c) Rajkumar
Write once, run anywhere.
Because 100% pure Java programs are compiled
into machine-independent bytecodes, they run
consistently on any Java platform.
9
Characteristics of Java
(c) Rajkumar
small and simple Platform
independent &
object-oriented
portable
distributed
High performance
Robust & Secure
multithreaded
architecture-neutral
dynamic
10
Simple and small
(c) Rajkumar
Java is very small and simple language. Java
does not use pointer and header files, goto
statements, etc. It eliminates operator
overloading and multiple inheritance.
11
Object Oriented
(c) Rajkumar
Object oriented programming is a way of
organizing programs as collection of objects, each
of which represents an instance of a class.
4 main concepts of Object Oriented programming are:
Abstraction
Encapsulation
Inheritance
Polymorphism
12
Distributed
(c) Rajkumar
Java is called as Distributed language for
construct applications on networks which can
contribute both data and programs. Java
applications can open and access remote
objects on Internet easily. That means multiple
programmers at multiple remote locations to
work together on single task.
13
Robust
(c) Rajkumar
Two main problems that cause program failures are
memory management mistakes and mishandled runtime
errors. Java handles both of them efficiently.
1) Memory management mistakes can be overcome by
garbage collection. Garbage collection is automatic de-
allocation of objects which are no longer needed.
2) Mishandled runtime errors are resolved by Exception
Handling procedures.
14
Secure
(c) Rajkumar
It provides a virtual firewall between the
application and the computer. Java codes are
confined within Java Runtime Environment
(JRE) thus it does not grant unauthorized
access on the system resources.
15
(c) Rajkumar
Programs run inside the virtual machine sandbox.
Array index limit checking. Code pathologies reduced by
bytecode verifier - checks classes after loading
o
class loader - confines objects to unique namespaces.
o
Prevents loading a hacked "java.lang.SecurityManager"
class, for example.
security manager - determines what resources a class can
o
access such as reading and writing to the local disk.
16
Java as Portable
(c) Rajkumar
Unlike other language compilers, Java complier
generates code (byte codes) for Universal Machine.
Java Virtual Machine (JVM): Interprets bytecodes at
runtime
ArchitectureNeutral
No Link Phase
Higher Level Portable Features: AWT, Unicode
17
Total Platform Independence
(c) Rajkumar
JAVA COMPILER
(translator)
JAVA BYTE CODE
(same for all platforms)
JAVA INTERPRETER
(one for each different system)
Windows 95 Macintosh Solaris Windows NT 18
High performance
(c) Rajkumar
Java performance is very extraordinary for an
interpreted language, majorly due to the
automatic garbage collector which runs as low-
priority background thread, ensuring a high
probability that memory is available when
required. Java architecture is also designed to
reduce overheads during runtime.
19
Multithreaded and Interactive
(c) Rajkumar
Multithreaded means managing multiple
tasks simultaneously. Java technologys
multithreading capability provides the means
to build applications with many concurrent
threads of activity.
20
Dynamic Binding and Extensible
(c) Rajkumar
Java is also dynamic language. Java is
capable of dynamically linking in new class,
libraries, methods and objects to where they are
located, is done at run-time. New classes can be
loaded while a program is running. Linking is
done on the fly.
21
JDK Versions
(c) Rajkumar
JDK 1.02 (1995)
JDK 1.1 (1996)
Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
JDK 8u91
22
JDK Editions
(c) Rajkumar
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
23
Java Keywords
(c) Rajkumar
24
(c) Rajkumar
An overview of the software development process.
25
Through the Java VM, the same application is
capable of running on multiple platforms.
(c) Rajkumar
26
(c) Rajkumar
27
Rules of Java Programming
(c) Rajkumar
Java code requires perfection. All words must be spelled
correctly.
Java is case-sensitive, meaning upper and lower case letters are
considered to be different characters.
Java ignores any white space such as blanks.
Curly braces are used for grouping. They mark the beginning
and end of programming sections
It is good coding practice to indent code within a block
Every Java statement will end with a semicolon.
28
Java Basic Structures
(c) Rajkumar A simple code that will print the words Hello World!
/* This is an example of basic java program.
/* This will print Hello World! as the output
*/
public class HelloWorldApp {
public static void main(String args[]) {
System.out.println("Hello World!"); // prints Hello
World!
}
}
29
Explanation of the Codes:
(c) Rajkumar
/* This is an example of basic java program.
* This will print Hello World! as the output */
// prints Hello World!
The following lines above are called COMMENT
Comments are ignored by the compiler
used for identifying the purpose of the program and the
programmer
Single line comment starts with //, just like the second statement
above
Multiple line comments begins with /* and ends with */, just like
the first statement above.
30
(c) Rajkumar public class HelloWorldApp {
The line above is the CLASS DECLARATION
Every Java program is a class. The program starts with the
name of the class. Class name must be the same as the .java file
in your project folder. In this case, file is saved as
HelloWorldApp.java
The keyword public is an access specifier
The word class is a keyword to define a new class and
HelloWorldApp is the name of the class
The opening curly brace ({ ) indicates the beginning of the
class
31
(c) Rajkumar
public static void main(String args[]) {
The line above is the MAIN METHOD
It is the starting point of a program. This part gets executed
first
The keyword static is a kind of modifier
Inside the parenthesis is the array of strings parameter
which is a necessity in the program
The opening curly brace ({ ) indicates the start of main
method
32
(c) Rajkumar System.out.println(Hello World!);
The line above is an example of an OUTPUT STATEMENT
The basic output statement in java are the System.out.println()
and the System.out.print()
System.out.println() prints what is between the double
quotation and move the printing cursor to the next line
System.out.print() - prints what is between the double
quotation and leave the printing cursor on the same line.
33
(c) Rajkumar
}
}
The code above is CLOSING BRACES
It is used to end the code block
The first brace ends the main method while the last one ends
the class
34
Escape Sequence in Java
(c) Rajkumar Escape sequence can be utilized when using the output
statements in Java.
Notation Character represented
\n newline
\r carriage return
\f form feed
\b backspace
\s Space
\t tab
\" double quote
\' single quote
\\ backslash
35
Common Java Derived Types (Classes)
Type
(c) Rajkumar
Description Common Methods
Considered as the root of the class
hierarchy. Every class has object as super clone(), equals(),
Object class (or parent class), and therefore, all fianlize(),hashCode(),notify
objects including arrays, implement the (),toString()
methods of this class.
Class containing a boolean field used for
booleanValue(),
Boolean wrapping a value of the primitive type
getBoolean(), valueOf()
boolean in an object.
charValue(), compareTo(),
Class containing a char field used for
getType(), isDigit(),
Character wrapping a value of the primitive type char
isLetter(), toLowerCase(),
in an object.
toUpperCase()
36
(c) Rajkumar
Class containing a double field used for doubleValue(),
Double wrapping a value of the primitive type parseDouble(0, toString(),
double in an object. valueOf()
Class containing a float field used for
floatValue(), parseFloat(0,
Float wrapping a value of the primitive type
toString(), valueOf()
float in an object.
Class containing a int field used for
intValue(), parseInt(0,
Integer wrapping a value of the primitive type int
toString(), valueOf()
in an object.
37
(c) Rajkumar
Class containing a long field used for wrapping a longValue(), parseLong(0,
Long
value of the primitive type long in an object. toString(), valueOf()
This represents character strings. All string charAt(), compareTo(),
String literals in Java programs, such as "abc" are concat(), length(),
implemented as instances of this class. startsWith(), endsWith()
It contains various methods for manipulating
arrays (such as sorting and searching). It also binarySearch(), fill(), sort(),
Arrays
contains a static factory that allows arrays to be equals()
viewed as lists.
38
(c) Rajkumar
brighter(), darker(),
This color class used encapsulates getAlpha(), getColor(),
Color
colors in the default RGB color space. getRed(), getGreen(),
getBlue()
getAttributes(), getFont(),
Font class represents fonts. It provides
getFontName(),
Font developers the ability to utilize more
getLineMatrics(0,
sophisticated typographic features.
getSize(), isBold(), isItalic()
39
(c) Rajkumar
Graphic class in the abstract base class for all
create(). drawRect(),
graphics contexts that allow an application to
drawOval(), drawString(0,
Graphics draw onto components that are relaized on
drawImage(), fillRect(),
various devices as well as onto off-screen
fillOval()
images.
add(), addAll(),
Vector class implements a growable array of
addElement(), elementAt(),
Vector objects. Like an array, it contains components
remove(), removeAll(),
that can be accessed using an integer index.
size(), toArray()
40