01 Introduction to Java
01 Introduction to Java
1101101010011010
Programming Languages
Machine Language Assembly Language High-Level Language
3
Programming Languages
Machine Language Assembly Language High-Level Language
4
Popular High-Level Languages
5
Interpreting/Compiling Source Code
A program written in a high-level language is
called a source program or source code.
Because a computer cannot understand a source
program, a source program must be translated
into machine code for execution. The
translation can be done using another
programming tool called an interpreter or a
compiler.
6
Interpreting Source Code
An interpreter reads one statement from the source
code, translates it to the machine code or virtual
machine code, and then executes it right away, as
shown in the following figure. Note that a statement
from the source code may be translated into several
machine instructions.
7
Compiling Source Code
A compiler translates the entire source code into a
machine-code file, and the machine-code file is
then executed, as shown in the following figure.
8
Java
v 1950: FORTRAN
v 1959: COBOL
Ø Half business software still programmed in COBOL
v 1971: Pascal
Ø Structured programming language
v 1983: ANSII C
v 2000: C#
Why Java?
15
Characteristics of Java
Java Is Simple Java is inherently object-oriented.
Although many object-oriented languages
Java Is Object-Oriented began strictly as procedural languages,
Java Is Distributed Java was designed from the start to be
object-oriented. Object-oriented
Java Is Interpreted programming (OOP) is a popular
Java Is Robust programming approach that is replacing
Java Is Secure traditional procedural programming
techniques.
Java Is Architecture-Neutral
Java Is Portable One of the central issues in software
development is how to reuse code. Object-
Java's Performance oriented programming provides great
Java Is Multithreaded flexibility, modularity, clarity, and
reusability through encapsulation,
Java Is Dynamic inheritance, and polymorphism.
16
Characteristics of Java
Java Is Simple Distributed computing involves several
computers working together on a network.
Java Is Object-Oriented Java is designed to make distributed
Java Is Distributed computing easy. Since networking
capability is inherently integrated into
Java Is Interpreted Java, writing network programs is like
Java Is Robust sending and receiving data to and from a
Java Is Secure file.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
17
Characteristics of Java
Java Is Simple You need an interpreter to run Java
programs. The programs are compiled into
Java Is Object-Oriented the Java Virtual Machine code called
Java Is Distributed bytecode. The bytecode is machine-
independent and can run on any machine
Java Is Interpreted that has a Java interpreter, which is part of
Java Is Robust the Java Virtual Machine (JVM).
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
18
Characteristics of Java
Java Is Simple Java compilers can detect many problems
that would first show up at execution time
Java Is Object-Oriented in other languages.
Java Is Distributed
Java has eliminated certain types of error-
Java Is Interpreted prone programming constructs found in
Java Is Robust other languages.
Java Is Secure
Java has a runtime exception-handling
Java Is Architecture-Neutral feature to provide programming support
Java Is Portable for robustness.
Java's Performance
Java Is Multithreaded
Java Is Dynamic
19
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java implements several security
Java Is Robust mechanisms to protect your system against
Java Is Secure harm caused by stray programs.
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
20
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral Write once, run anywhere
Java Is Portable With a Java Virtual Machine (JVM),
Java's Performance you can write one program that will
run on any platform.
Java Is Multithreaded
Java Is Dynamic
21
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable Because Java is architecture neutral,
Java programs are portable. They can
Java's Performance be run on any platform without being
Java Is Multithreaded recompiled.
Java Is Dynamic
22
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable Java’s performance Because Java is
architecture neutral, Java programs are
Java's Performance portable. They can be run on any
Java Is Multithreaded platform without being recompiled.
Java Is Dynamic
23
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance Multithread programming is smoothly
Java Is Multithreaded integrated in Java, whereas in other
Java Is Dynamic languages you have to call procedures
specific to the operating system to enable
multithreading.
24
Characteristics of Java
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance Java was designed to adapt to an evolving
environment. New code can be loaded on the
Java Is Multithreaded fly without recompilation. There is no need for
developers to create, and for users to install,
Java Is Dynamic major new software versions. New features can
be incorporated transparently as needed.
25
JDK Versions
JDK 1.02 (1995)
JDK 1.1 (1996)
JDK 1.2 (1998)
JDK 1.3 (2000)
JDK 1.4 (2002)
JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
JDK 1.7 (2011) a. k. a. JDK 7 or Java 7
JDK 1.8 (2014) a. k. a. JDK 8 or Java 8
26
JDK Editions
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, Java ServerPages, and Java
ServerFaces.
Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
27
Popular Java IDEs
NetBeans
Eclipse
The Java Buzzwords
üExamples:
§ int a, b, c;
§ int d = 3, e, f = 5;
§ byte g = 22;
§ double pi = 3.14159;
§ char ch = 'x';
Variable Scope
Øtype array-variable[];
vint monthDays[] =
{31,28,31,30,31,30,31,31,30,31,30,31};
vNote:
Ø 1) there is no need to use the new operator
Ø 2)the array is created large enough to hold all
specified elements
Multidimensional Arrays
Ø Examples:
72
animation
73
animation
74
animation
75
Java Programs
Invoking:
There is NO explicit invocation statement needed: When the
object creation statement is executed, the constructor method
will be executed automatically.
Defining a Constructor: Example
public class Counter {
int CounterIndex;
// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or access counter
public void increase()
{
CounterIndex = CounterIndex + 1;
}
public void decrease()
{
CounterIndex = CounterIndex - 1;
}
int getCounterIndex()
{
return CounterIndex;
}
}
A Counter with User Supplied
Initial Value ?
This can be done by adding another constructor method
to the class.
public class Counter {
int CounterIndex;
// Constructor 1
public Counter()
{
CounterIndex = 0;
}
public Counter(int InitValue )
{
CounterIndex = InitValue;
}
}
// A New User Class: Utilising both constructors
Counter counter1 = new Counter();
Counter counter2 = new Counter (10);
Multiple Constructors: Example
public class Circle {
public double x,y,r; //instance variables
// Constructors
public Circle(double centreX, double cenreY, double radius) {
x = centreX; y = centreY; r = radius;
}
public Circle(double radius) { x=0; y=0; r = radius; }
public Circle() { x=0; y=0; r=1.0; }
Purpose
Find and delete unreachable objects.
Free space as much as possible.
When??
GC is under control of JVM.
One can request but no guarantees.
How??
Discovery of unreachable objects.
With the help of Algorithms.
Garbage Collection in Java
Basic Approaches
vReference Counting
vTracing
vCompacting
vCopying
vGenerational
vThe Train Algorithm
Binding
vAchieved at runtime
ØWhen the class of an object cannot be determined at
compile time
ØMeans the JVM (not the compiler) must bind a
method call to its implementation
vInstances of a sub-class can be treated as if they were
an instance of the parent class
ØTherefore the compiler doesn’t know its type, just
its base type.
Dynamic Binding Summary
void outerMethod() {
System.out.println("Outer Method");
}
class InnerClass {
void display() {
// Accessing outer class's private field and method
System.out.println("Accessing: " + outerField);
outerMethod();
}
}
For example:
class LinkedList {
static class Node {
Object head;
Node tail;
}
Node head;
}
Example of Nested Classes
Scenario: Company and Department
Let's consider a Company that has multiple Departments. the
Company class contains a static nested class Department. The
Department class represents different departments within the
company and is independent of any specific instance of Company.
ØThe Company class need not have any instance variables or
methods directly related to departments.
ØThe static nested class Department represents different departments
within the company.
ØThe Department class has instance variables name and manager
and a method displayDepartment to display department details.
ØThe static nested class can be used to create and manage department
instances independently of any specific Company instance.
Example of Nested Classes
class OuterClass {
private static String staticOuterField = "Static Outer Field";
class Container {
double width;
double height;
double depth;
Container() {
System.out.println("Constructing Container");
width = 10; height = 10; depth = 10;
}
doublevolume() {
return width * height * depth;
}
}
Parameterized Constructor
class Container {
double width;
double height;
double depth;
Container(double w, double h, double d) {
width = w; height = h; depth = d;
}
double volume()
{ return width * height * depth;
}
}
Methods
class Container {
double width, height, depth;
void volume() {
System.out.print("Volume is ");
System.out.println(width * height *
depth);
}
}
Parameterized Method
void concreteMethod() {
System.out.println("Concrete method called.");
}
}
ØException Handling
ØAdvantage of Exception Handling
ØHierarchy of Exception classes
Types of Exception
1. Checked Exception
2. Unchecked Exception
3. Error
5. Scenarios where exception may occur
Exception Types
System Errors
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}}
Restrictions:
May not span multiple lines.
"This is not
a legal String."
May not contain a " character.
"This is not a "legal" String either."
Escape sequences
escape sequence: A special sequence of characters
used to represent certain special characters in a
string.
\t tab character
\n new line character
\" quotation mark character
\\ backslash character
Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
Output:
\hello
Questions
What is the output of the following println
statements?
System.out.println("\ta\tb\tc");
System.out.println("\\\\");
System.out.println("'");
System.out.println("\"\"\"");
System.out.println("C:\nin\the
downward spiral");