Object Oriented Java
Programming (OOJ)
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.
Where it is used?
According to Sun, 3 billion devices run java. There are
many devices where java is currently used. Some of
them are as follows:
• Desktop Applications such as acrobat reader, media
player, antivirus etc.
• Web Applications such as irctc.co.in, javatpoint.com
etc.
• Enterprise Applications such as banking applications.
• Mobile
• Embedded System
• Smart Card
• Robotics
• Games etc.
Java is Interpreted
• Java is compiled to an intermediate "byte code" at compilation time.
• This is in contrast to a language like C that is compiled to machine language at compilation time.
• The Java byte code cannot be directly executed on hardware the way that compiled C code can.
• Instead the byte code must be interpreted by the JVM (Java Virtual Machine) at runtime in order to be
executed
The Java Buzzwords
• Simple - Java was designed to be easy for the professional programmer to learn
and use effectively.
• Secure -Java programs run inside a virtual machine which is known as a
sandbox. Java does not support explicit pointer. Byte-code verifier checks the code
fragments for illegal code that can violate access right to object.
• Portable
•Object-oriented - The object model in Java is simple and easy to extend,
while primitive types, such as integers, are kept as high performance non objects.
• Robust- The multiplatformed environment of the Web places extraordinary
demands on a program, because the program must execute reliably in a variety of
systems. Because Java is a strictly typed language, it checks your code at compile
time. However, it also checks your code at run time.
Java is robust, consider two of the main reasons for program failure: memory
management mistakes and mishandled exceptional conditions (that is, run-time
errors).
• Multithreaded
• Architecture-neutral - A central issue for the Java
designers was that of code longevity and portability.
• Interpreted - Java enables the creation of cross-
platform programs by compiling into an intermediate
representation called Java bytecode. This code can be executed
on any system that implements the Java Virtual Machine.
• High performance -the Java bytecode was carefully
designed so that it would be easy to translate directly into
native machine code for very high performance by using a just-
in-time compiler. Java run-time systems that provide this
feature lose none of the benefits of the platform-independent
code.
• Distributed - Java is designed for the distributed
environment of the Internet because it handles TCP/IP protocols.
In fact, accessing a resource using a URL is not much different
from accessing a file. Java also supports Remote Method
Invocation (RMI). This feature enables a program to invoke
methods across a network.
• 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 Java
environment, in which small fragments of bytecode may be
dynamically updated on a running system.
Types of Java Applications
There are mainly 4 type of applications that can be
created using java programming:
1) Standalone Application
• It is also known as desktop application or window-
based application. An application that we need to
install on every machine such as media player,
antivirus etc. AWT and Swing are used in java for
creating standalone applications.
2) Web Application
• An application that runs on the server side and
creates dynamic page, is called web application.
Currently, servlet, jsp, struts, jsf etc. technologies are
used for creating web applications in java.
3) Enterprise Application
• An application that is distributed in nature,
such as banking applications etc. It has the
advantage of high level security, load
balancing and clustering. In java, EJB is used
for creating enterprise applications.
4) Mobile Application
• An application that is created for mobile
devices. Currently Android and Java ME are
used for creating mobile applications.
Two Paradigms for Computer Programs
There are two paradigms for a program construction:
Process Oriented Model
This model can be thought of as code acting on data
Procedural languages, such as C, characterize a series of
linear steps (that is, code)
Object- Oriented Programming
Aims to manage increasing complexity.
Organizes a program around its data (that is object) and a set of
well-defined interfaces to that data.
This program can be characterized as data controlling
access to code.
Object-Oriented Programming (OOP)
OOP is the core of Java
All Java programs are object-oriented.
This isn’t the option the way that it is in C++
OOP and Java are integrated, it is important to understand its
basic principles before writing java programs
Abstraction
The essential element of OOP is abstraction
It is the process of focusing on those features of something that are essentials for
the task at hand and ignoring those that are not.
For a personal system, we are interested only in people objects and only the
ones that are employed by the company. The skiers, golfers, and cyclists are not
included.
The powerful way to manage abstraction is through the use of hierarchical
classifications
This allows us to layer semantic of complex systems, breaking them into more
manageable pieces.
From the outside, the car is a single object. Once inside, we see that car
consists of several subsystems: steering, clutch pedal, brakes, sound
system, seat belts, heading,, and so on.
Each subsystem is made of more specialized units.
The sound system consists of a radio, CD player, a tape player
Hierarchical Abstractions
We have to manage the complexity of system through the use of
hierarchical abstractions.
The data from a traditional process-oriented program can be
transformed by abstraction into its component objects.
A sequence of process steps can become a collection of messages
between these objects.
Each of these objects describes its own unique behavior.
We can threat objects as concrete entites that respond to
messages telling them to do something
This is the essence of object-oriented programming.
It is important that we understand how the
object-oriented concepts, forming the heart of
Java, translate into programs
All OOP languages provide mechanism that help us
to implement OO model they are
Encapsulation
Inheritance
Polymorphism
A programming language is said to support OO design
if it supports these three concepts in its syntax
A First Simple Program
/*
This is a simple Java program.
Call this file "Example.java".
*/
import java.lang.*;
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}
Java System.out.println() is used to print an argument that is passed to it. The
statement can be broken into 3 parts which can be understood separately as:
System: It is a final class defined in the java.lang package.
out: This is an instance of PrintStream type, which is a public and static member
field of the System class.
println(): As all instances of PrintStream class have a public method println(),
hence we can invoke the same on out as well. This is an upgraded version of
print(). It prints any argument passed to it and adds a new line to the output. We
can assume that System.out represents the Standard Output Stream.
Compiling the 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
bytecode version of the program.
• The Java bytecode is the intermediate representation of your program that
contains instructions the Java Virtual Machine will execute.
• Thus, the output of javac is not code that can be directly executed.
• To actually run the program, you must use the Java application launcher, 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:
This is a simple Java program.
• When Java source code is compiled, each individual class is put into its own output file
named after the class and using the .class extension.
• This is why it is a good idea to give your Java source files the same name as the class
they contain—the name of the source file will match the name of the .class file.
• When you execute java as just shown, you are actually specifying the name of the class
that you want to execute.
• It will automatically search for a file by that name that has the .class extension. If it
finds the file, it will execute the code contained in the specified class
/*
Here is another short example.
Call this file "Example2.java".
*/
class Example2 {
public static void main(String args []) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
output:
This is num: 100
The value of num * 2 is 200
Two Control Statements
The if Statement
if(condition) statement;
class IfSample {
public static void main(String args[]) {
int x, y;
x = 10;
y = 20;
if(x < y) System.out.println("x is less than y");
x = x * 2;
if(x == y) System.out.println("x now equal to y");
x = x * 2;
if(x > y) System.out.println("x now greater than y");
// this won't display anything
if(x == y) System.out.println("you won't see this");
}
}
Output
x is less than y
x now equal to y
x now greater than y
The for Loop
for(initialization; condition; iteration) statement;
class ForTest {
public static void main(String args[]) {
int x;
for(x = 0; x<10; x = x+1)
System.out.println("This is x: " + x);
}
}
output:
This is x: 0
This is x: 1
This is x: 2
This is x: 3
This is x: 4
This is x: 5
This is x: 6
This is x: 7
This is x: 8
This is x: 9
Java Is a Strongly Typed Language
• every variable has a type, every expression has a type, and
every type is strictly defined.
• all assignments, whether explicit or via parameter passing in
method calls, are checked for type compatibility.
Primitive Data Types
Java defines eight primitive types of data:
byte, short, int, long, char, float,
double and boolean.
Primitive Data Types
These can be put in four groups:
• Integers This group includes byte, short, int, and long, which are for
whole-valued signed numbers.
• Floating-point numbers This group includes float and double, which
represent numbers with fractional precision.
• Characters This group includes char, which represents symbols in a
character set, like letters and numbers.
• Boolean This group includes boolean, which is a special type for
representing true/false values.
Appendix A: Introduction to Java 50
// Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days
here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
output:
In 1000 days light will travel about 16070400000000 miles.
// Compute the area of a circle.
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
output:
ch1 and ch2: X Y
// char variables behave like integers.
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
Output
ch1 contains X
ch1 is now Y
// Demonstrate boolean values.
class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Output
b is false
b is true
This is executed.
10 > 9 is true
References
•Java the Complete Reference, Herbert Schildt, Tata McGraw-hill Edition
•Internet(WWW)