WELCOME TO RICHFIELD
HYBRID LEARNING
MODULE: PROGRAMMING 731 (Java)
TOPIC 1: INTRODUCTION OF JAVA LANGUAGE AND OOP
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 1
Learning Outcomes:
After studying this topic you should be able to:
• Describe features of Java Programming
• Difference between The Java Applets and Applications.
• Understand Importance of JVM.
• Explain Object Oriented Programming Concepts.
• Create a simple Java Program.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 2
TOPIC 1:
INTRODUCTION OF JAVA LANGUAGE AND OBJECT ORIENTED
PROGRAMMING
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 3
What is Java?
It is an object-oriented language developed by Sun in the mid
1990s.
Original language called Oak
Intended for embedded systems
• Unlike C++, it was developed from scratch.
The syntax is very similar to C.
Sun describes it as
"A simple, object-oriented, distributed, interpreted, robust, secure,
architecture neutral, portable, high-performance, multi-threaded and
dynamic language."
As unbelievable as it may sound, Java will not save the world!
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 4
What is Java? (cont)
Object-Oriented
Designed to support Object-Oriented concepts
However, does contain non-Object-Oriented primitive data types
Distributed
Applications are constructed using objects. Objects can be
distributed in multiple locations within a network environment.
Extensive integration with TCP/IP
Interpreted
Java compiles to byte-code (not machine code). Byte code is
interpreted.
Most Java versions after 1.2 include a JIT (Just-In-Time) compiler
which compiles byte code to machine code.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 5
What is Java? (cont)
Robust
Memory management is done automatically
Use of pointers is limited
Secure
All Java code subject to security model.
Architecture-Neutral/Portable
Compiled Java (byte code) will run on any platform which has a
Java Virtual Machine
The Java Virtual Machine is available for almost all platforms...
Even mainframes.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 6
What is Java? (cont)
High-Performance
Originally, Java's performance was poor.
Now, Java's performance rivals C++.
Multi-Threaded
Processes contain multiple threads of execution.
Similar to multi-tasking but all threads share the same memory
space.
Dynamic
Makes heavy use of dynamic memory allocation.
Classes can be dynamically loaded at any time.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 7
Platform Independence. How does Java do it?
Java has been described as WORA (Write once, Run Anywhere)
In most cases, this is true.
Not always true with GUI.
Doesn't always come for free. Can require a lot of testing.
Because Java source code is compiled to byte code and the byte
code is interpreted, Java code can be executed anywhere an
interpreter is available.
The "Interpreter" is call the Java Virtual Machine
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 8
Benefits of Java
• Object-oriented
• Improved on C++
• Security
• APIs (Application Programming Interface)
‒ Lots of prewritten code
• Distributed
‒ Able to run over the network
• “Write once, run anywhere”
‒ Platform independent
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 9
Java Programs
• Application
‒ Runs natively on the system through the Java Virtual Machine (JVM)
• Applet
‒ Runs in a browser
‒ Does smaller tasks
• Programs
‒ Both applets and applications are “Java programs”
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 10 10
The Java Virtual Machine.
Traditionally, source code had to be compiled for the target hardware
and OS platform:
Windows i386 binary
Compiler
Solaris SPARC binary
[Link]
Compiler
Mac PPC binary
Compiler
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 11
The Java Virtual Machine.
Java source files (.java) are compiled to Java bytecode (.class)
Bytecode is interpreted on the target platform within a Java Virtual
Machine
i386 VM
Java Java
[Link] SPARC VM
Compiler Bytecode
[Link]
PPC VM
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 12
Java Terminology
• Java Virtual Machine (JVM)
• Java Runtime Environment (JRE)
• Application Programming Interface (API)
‒ Consists of pre-written code that you can use
‒ Example: DirectX, OpenGL
• Java Developers Kit (JDK)
‒ Old name for Java technology
• Java 2, Java 2 platform, Java 2 SDK
‒ New name for Java technology
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 13 13
Java Terminology
Java SDK
JRE
JVM
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 14
Java VM Responsibilities
The Java VM does more than interpret bytecode:
The class loader loads appropriate java classes. Possibly from the
network.
All classes are verified to contain only legal bytecodes and not
permitted any illegal stack or register usage.
A SecurityManager can limit access to resources such as the local
file system or the network.
Any unreferenced memory (Objects) are returned to the system by
the Garbage Collector thread.
• Many database servers, application servers, web servers and
browsers contain a Java virtual machine
• eg: Oracle, Tomcat (web server), WebSphere (app server), BEA
Weblogic (app server), and Netscape and IE.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 15
The Java Software Development Kit (SDK)
The Java SDK comes in three versions:
J2ME - Micro Edition (for handheld and portable devices)
J2SE - Standard Edition (PC development)
J2EE - Enterprise Edition (Distributed and Enterprise Computing)
• The SDK is a set of command line tools for developing Java
applications:
• javac - Java Compiler
• java - Java Interpreter (Java VM)
• appletviewer - Run applets without a browser
• javadoc - automated documentation generator
• jdb - Java debugger
• The SDK is NOT and IDE (Integrated Development Environment)
• Command line only. No GUI.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 16
Integrated Development Environments (IDEs)
There are many IDEs available. Some are public domain and
some are commercial:
Symantic Visual Cafe
JBuilder
IBM Visual Age
Kawa
Forte for Java
Many OO modelling tools (such as Together Control Center)
include an IDE.
Most IDEs offer a "demo" mode so you can try before you buy.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 17
Obtaining the Java SDK
Download from Sun Web-site:
[Link]
Select "J2SE downloads"
Choose your version
Select your platform
• Download will be an installer file appropriate for your platform:
• Installer .exe for windows
• rpm or self extracting file for linux
• tar or self extracting file for SPARC
• To install, execute the installer program or extract from tar file.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 18
Commonly Used Packages
While it should be your goal to learn as many packages as you
can, there are some packages you will use more than others:
Language [Link] Common classes used for all
(general) application development
GUI [Link] Graphical User Interface,
[Link] Windowing,
[Link] Event processing
Misc. Utilities [Link] Helper classes, collections
and Collections
Input/Output [Link] File and Stream I/O
Networking [Link] Sockets, Datagrams
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 19
[Link]
Here is Java's "HelloWorld" implementation:
In the file, [Link]:
public class HelloWorld
{
public static void main(String[] args)
{
[Link]("Hello World");
}
}
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 20
Running HelloWorld
To compile [Link], use the compiler. If successful, it will
produce a file called [Link] in the same directory.
> javac [Link]
[ compiler output ] errors and warnings
To execute, run the Java VM and include the name of the class
which contains the "main" method as the first command line
parameter.
> java HelloWorld
Hello World note: do not include the .class extension
output from program
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 21
Comments
• A comment can begin with //.
• Everything after these symbols and to the end of the line is
treated as a comment and is ignored by the compiler.
class HelloWorld { // My first program
public static void main(String[] args) {
// Print out the words - Hello World
[Link]("Hello World!");
}
}
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 22
Comments
• A comment can begin with /* and end with */
• Everything between these symbols is treated as a comment and
is ignored by the compiler.
/*
Hello World by Trina Gregory
*/
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
}
}
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 23
Comments
• A javadoc comment, begins with /** and ends with */.
• It can be extracted automatically from Java software.
/**
* Hello World program
* @author Sam
*/
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
}
}
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 24
When to Use Comments
• Begin each program file with an explanatory comment
‒ What the program does
‒ The name of the author (YOU)
‒ Contact information for the author (email)
‒ Date of creation or the last modification
• Provide only those comments which the expected reader of the
program file will need in order to understand it.
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 25
Example
/*
* Sam ram
* ITP 109 Fall 2011, 8/30/2011
* HelloWorld program
*/
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World!");
}
}
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 26 26
Bugs and Debugging
• Bugs:
‒ Program does not compile
‒ Program produces output that is not expected when it executes
• Debugging is the act of removing them
• A programmer spends about half of his/her time debugging code
• Compiler error
• Executing (or run-time) error
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 27 27
Review
What are the key features of Java?
How does Java obtain platform independence?
What is the Java Virtual Machine and what are its responsibilities?
What is the Java SDK? What is the JRE?
What is the Java API?
What are packages?
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 28
THANK YOU
[ADD PRESENTATION TITLE IN SLIDE MASTER MODE] | 29