unit1
unit1
JAVA PROGRAMMING
INTRODUCTION
Course Objectives
Organization of chapters in
unit
2
COURSE OBJECTIVES FOR
CHAPTER 1
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
4
SUB TITLE CHAPTERS
Part I: Fundamentals of Programming
5
SUB CHAPTERS, CONT.
Part II: Object-Oriented Programming
6
CHAPTER 1 INTRODUCTION TO
JAVA
What Is Java?
Getting Started With Java Programming
7
WHAT IS JAVA?
History
Characteristics of Java
8
HISTORY
1. James Gosling and Sun Microsystems
2. Oak
4. Hot Java
The first Java-enabled Web browser
5. JDK Evolutions
6. J2SE, J2ME, and J2EE (not mentioned in the book, but could
discuss here optionally)
9
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
10
JDK VERSIONS
11
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 Server Pages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
13
GETTING STARTED WITH JAVA
PROGRAMMING
14
A SIMPLE APPLICATION
Example 1.1
//This application program prints Welcome
//to Java!
package chapter1;
Source Run
On command line
javac file.java Source Code
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
16
If runtime errors or incorrect result
EXECUTING APPLICATIONS
On command line
java classname
Bytecode
17
EXAMPLE
javac Welcome.java
java Welcome
output:...
18
COMPILING AND RUNNING A
PROGRAM
• Where are the
files stored in
Welcome.java
c:\example the directory?
chapter1 Welcome.class
Welcome.java~
.
.
.
chapter19 Java source files and class files for Chapter 19
19
ANATOMY OF A JAVA PROGRAM
Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
20
COMMENTS
• In Java, comments are preceded by two slashes
(//) in a line, or enclosed between /* and */ in
one or multiple lines.
• When the compiler sees //, it ignores all text
after // in the same line.
• When it sees /*, it scans for the next */ and
ignores any text between /* and */.
21
RESERVED WORDS
• Reserved words or keywords are words that
have a specific meaning to the compiler and
cannot be used for other purposes in the
program.
• For example, when the compiler sees the word
class, it understands that the word after class is
the name for the class.
• Other reserved words in Example 1.1 are
public, static, and void. Their use will be
introduced later in the book.
22
MODIFIERS
• Java uses certain reserved words called modifiers
that specify the properties of the data, methods,
and classes and how they can be used.
• Examples of modifiers are public and static. Other
modifiers are private, final, abstract, and
protected.
• A public datum, method, or class can be
accessed by other programs. A private datum or
method cannot be accessed by other programs.
• Modifiers are discussed in Chapter 6, "Objects
and Classes."
23
STATEMENTS
• A statement represents an action or a
sequence of actions.
• The statement System.out.println("Welcome
to Java!") in the program in Example 1.1 is a
statement to display the greeting "Welcome to
Java!" Every statement in Java ends with a
semicolon (;).
24
BLOCKS
25
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.
26
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.
27
MAIN METHOD
The main method provides the control of program
flow. The Java interpreter executes the application
by invoking the main method.
28