Introduction To Java Programming
Introduction To Java Programming
Introduction To Java Programming
Java Programming
Introduction
Course Objectives
2
Course Objectives
Upon completing the course, you will understand
– Create, compile, and run Java programs
– Primitive data types
– Java control flow
– Methods
– Arrays (for teaching Java in two semesters, this could be the end)
– Object-oriented programming
– Core Java classes (Swing, exception, internationalization,
multithreading, multimedia, I/O, networking, Java
Collections Framework)
3
Course Objectives, cont.
You will be able to
– Develop programs
– Write simple programs using primitive data
types, control statements, methods, and arrays.
– Create and use methods
– Develop a GUI interface and Java applets
– Write interesting projects
– Establish a firm foundation on Java concepts
4
Programming Concepts
What is JAVA?
5
Programming in the Big Picture?
What are Computers Used For?
- Word Processing (i.e., typing up some documents and
printing them)
– Business Applications (i.e., accounting, spreadsheets,
presentations,...)
– Engineering Applications (i.e., scientific analysis,
simulations)
– Database Management (i.e., police records, stock market, ...)
– Entertainment (i.e., games, multimedia applications, ...)
– Manufacturing (i.e., CAD/CAM, robotics, assembly, ...)
– Many more things
6
Who is Involved With Computers ?
System/Hardware Designers (The people that design
computers and related products)
Manufacturers (The people that actually build and
assemble computers)
Software Designers (The people that design
applications to be used with the computers)
Programmers (The people that write computer
programs to achieve working applications, games and
other software packages).
User (The people that buy and use the software)
7
– We are going to play the part of the Programmer
in this course, although we will also be designing
our programs (software) and hence we will also be
the Software Designer. When we test our
programs, we'll pretend that we are the User.
Testing is an important part of programming to
ensure that the User is happy with software that is
not full of Bugs!
8
What is program?
A program is traditionally known as
– a sequence of instructions that can be executed by a
computer to solve some problem.
Here, we will learn to write our own programs to
solve some very simple problems.
There are different styles (types) of programming:
– procedural (structured)
– logical (specifying constraints and conditions)
– object-oriented
9
What is Object Oriented Programming ?
10
For example, consider the following
programming scenario and try to come up with
some objects that might be used in an object
oriented program. Also, try to think of what
operations might be needed to interact with the
objects. That is, what information do we need to
know about the objects and how do we get the
information ?
11
Example: An Automated Bank Teller
Machine (ATM)
OBJECTS OPERATIONS
Money Withdraw or Deposit
Buttons Is a button being pressed ? In what Order ?
Display Window Display greeting, main menu, withdrawal message,...
Deposit Envelope What's in it: cash or cheque ?
Bank Card Has one been inserted properly ? What account is it for ?
13
What Is Java?
History
Characteristics of Java
JAVA is:
– An programming language from SUN Microsystems.
– Object-Oriented
– Interpreted
14
History
James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
HotJava
– The first Java-enabled Web browser
JDK Evolutions
J2SE, J2ME, and J2EE (not mentioned in the
book, but could discuss here optionally)
15
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 is multithreaded
Java is dynamic
16
JDK Versions
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)
17
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 and Java ServerPages.
Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
19
When programming in JAVA, you will usually use the
following building blocks:
classes from the JAVA class libraries
classes you create yourself
classes that other people make available to you
20
How does JAVA really work ?
21
Getting Started with Java
Programming
A Simple Java Application
Compiling Programs
Executing Applications
22
A Simple Application
Example 1.1
23
Creating and Compiling Programs
On command line
– javac file.java
24
Executing Applications
On command line
– java classname
Bytecode
25
Example
javac Welcome.java
java Welcome
output:...
26
Compiling and Running a Program
Where are the files
Welcome.java
stored in the
c:\example directory?
chapter1 Welcome.class
Welcome.java~
.
.
.
chapter19 Java source files and class files for Chapter 19
27
Anatomy of a Java Program
Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
28
Comments
33
Blocks
A pair of braces in a program forms a block
that groups components of a program.
34
Classes
The class is the essential Java construct. A class
is a template or blueprint for objects. To
program in Java, you must understand classes
and be able to write and use them. The mystery
of the class will continue to be unveiled
throughout this book. For now, though,
understand that a program is defined by using
one or more classes.
35
Methods
What is System.out.println? It is a method: a
collection of statements that performs a sequence of
operations to display a message on the console. It can
be used even without fully understanding the details
of how it works. It is used by invoking a statement
with a string argument. The string argument is
enclosed within parentheses. In this case, the
argument is "Welcome to Java!" You can call the
same println method with a different argument to
print a different message.
36
main Method
The main method provides the control of
program flow. The Java interpreter executes the
application by invoking the main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
37
Displaying Text in a Message
Dialog Box
you can use the showMessageDialog
method in the JOptionPane class.
JOptionPane is one of the many
predefined classes in the Java system,
which can be reused rather than
“reinventing the wheel.”
38
The showMessageDialog Method
JOptionPane.showMessageDialog(null, "Welcome
to Java!","Example 1.2",
JOptionPane.INFORMATION_MESSAGE));
39
The exit Method
Use Exit to terminate the program and stop
all threads.