java programming fundamentals. lesson 1: java runtime environment

164
Java Programming Fundamentals

Upload: pierce-parsons

Post on 02-Jan-2016

289 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Programming Fundamentals

Page 2: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 1:Java Runtime Environment

Page 3: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Identify the differences between stand-alone applications and applets

Describe the role of the Java Virtual Machine

Create a simple Java program

Use Java comments

Compile and execute a Java program

Describe the differences between *.java and *.class files

Page 4: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

The Java Virtual Machine

Stand-alone applications

Applets

Classes

Methods

Bytecode

Page 5: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Development Cycle

*.java *.classOutput

Window

Text Editor javac java

Page 6: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Comments

Single-line comment

Multiline comment

Javadoc comment

Page 7: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Identify the differences between stand-alone applications and applets

Describe the role of the Java Virtual Machine

Create a simple Java program

Use Java comments

Compile and execute a Java program

Describe the differences between *.java and *.class files

Page 8: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 2:Data Types,

Variables and Operators

Page 9: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Use primitive data types

Declare a variable

Distinguish between local and class variables

Distinguish between implicit and explicit casting

Use the operators for primitive types

Page 10: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Data Types

Primitives

- Integers

- Floating-point numbers

- Character

- Boolean

Page 11: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Declaring Variablesand Variable Scope

Types

- Local

- Instance

- Class

Default variable values

Variable declaration and initialization

Casting

- Implicit

- Explicit

Page 12: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Casting Rules Chart

byte short int long float double

char

Page 13: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Operators

Arithmetic operators

Relational operators

Logical operators

- Short-circuit

Bitwise operators

Page 14: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

AutomaticCasting

Java automatically casts

-bytes or shorts ints-floats doubles

Page 15: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Use primitive data types

Declare a variable

Distinguish between local and class variables

Distinguish between implicit and explicit casting

Use the operators for primitive types

Page 16: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 3:Control

Statements

Page 17: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Explain the block structure of Java

Use Java conditional statements

Use Java iterative statements

Page 18: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Code Blocks

Used to group the contents of a class or a method

Used to designate all conditional and iterative statements

Used for exception handling

Page 19: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Conditional Statements

if statement

switch/case statement

-break statement

Page 20: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Iterative Statements (Loops)

while loop (entry condition loop)

do while loop (exit condition loop)

for loop

Nested loops (break and continue)

Page 21: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Explain the block structure of Java

Use Java conditional statements

Use Java iterative statements

Page 22: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 4:Methods

Page 23: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Create and use static methods

Return a value from a method

Explain pass by value

Describe overloading methods

Identify the method signature

Page 24: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

JavaMethods

The executable part of a class

- Member

Page 25: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Return Statement

Data types

Void

Page 26: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Calling a Method

Parameters

Literals

Page 27: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Pass by Value

A copy of the value of the variable is available to the method; changes to the copy do not affect the original value

Page 28: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Overloading

Method signature

- Name

- Parameter list

Does not include return type

Page 29: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Create and use static methods

Return a value from a method

Explain pass by value

Describe overloading methods

Identify the method signature

Page 30: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 5:Arrays

Page 31: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Declare and initialize an array

Allocate space for a new array

Describe array indexing

Use the length property

Discuss why arrays are passed by reference

Discuss Java’s garbage collection mechanism

Retrieve command line parameters

Page 32: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Array?

Collection of same type

Non-primitive data type

Page 33: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Initializing an Array

Allocation

Reference

Index

Page 34: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objects

Properties

Methods

Page 35: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Using an Array

length property

Page 36: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

1

0

3

1

5

2

MyIntArray

tmpIntArray

Passing an Array to a Method

Page 37: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Garbage Collection

Variable name reuse

Memory leak

Page 38: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Command Line Parameters

Always String type data

Page 39: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Declare and initialize an array

Allocate space for a new array

Describe array indexing

Use the length property

Discuss why arrays are passed by reference

Discuss Java’s garbage collection mechanism

Retrieve command line parameters

Page 40: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 6:Classes

and Objects

Page 41: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Identify the parts of an object

Create and use instance members

Distinguish between instance and class members

Define abstraction

Create object references

Page 42: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Object-Oriented Programming

Refers to the creation of a number of objects that communicate with one another

Page 43: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Isan Object?

Instantiation

The new keyword

Page 44: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Instance andClass Members

Class members

- The exception in Java

Instance members

- The rule in Java

Accessing instance members

- Dot notation

Accessing class members

Page 45: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Abstraction

What does the object do versus how does it do it?

Functionality versus implementation

Page 46: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Object References

rachael

tmpEmp

name: "Rachael"dept: "Accounting"salary: 52000

tmpEmp variable

Page 47: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Identify the parts of an object

Create and use instance members

Distinguish between instance and class members

Define abstraction

Create object references

Page 48: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 7:Inheritance

Page 49: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Create a new class using inheritance

Create an overridden method

Page 50: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is Inheritance?

Code reuse

Using inheritance

-extends keyword

- subclass

- superclass

- Multiple inheritance

Page 51: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Overriding Methods

Same signature as the superclass

super.method()

Page 52: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Create a new class using inheritance

Create an overridden method

Page 53: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 8:Constructors

Page 54: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Use the default constructor

Create a constructor to initialize instance variables

Call other constructors from both the same class and the parent class

Create a no-arguments constructor

Discuss String characteristics and define the common methods of String

Page 55: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is a Constructor?

new keyword

Similarities to methods

- Initialization of object

- Pass parameters

Differences from methods

- Same name as class

- No return type

Callback

Page 56: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Using Constructors

Default constructor

- No arguments

- Must be explicitly created if there are any other constructors

Overloading constructors

- Unique parameter types

Page 57: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

The Keyword this

this() as a constructor

Avoiding namespace conflicts

The super keyword

Page 58: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Constructors and Callbacks

Constructors are a major facilitator in establishing interobject communication

Page 59: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Strings and StringBuffer

String constructors

String characteristics

Methods of String

StringBuffer

Page 60: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Use the default constructor

Create a constructor to initialize instance variables

Call other constructors from both the same class and the parent class

Create a no-arguments constructor

Discuss String characteristics and define the common methods of String

Page 61: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 9:Interfaces and

Abstract Classes

Page 62: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define and use interfaces

Define polymorphism

Use abstract classes

Create an abstract method

Page 63: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Interface?

Contents of an interface

- Abstract methods

- Final variables

Interface functions

- Decoupling objects

- Providing data type

Page 64: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Polymorphism

Using one method name to invoke many different methods

Page 65: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Abstract Class?

Cannot be instantiated

Can be used for a type

Page 66: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define and use interfaces

Define polymorphism

Use abstract classes

Create an abstract method

Page 67: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 10:Packages and

Access Modifiers

Page 68: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Compile and run a class with packages

Identify the packages in the Java 2 API

Identify the various access levels in Java

Describe how access modifiers can be used to set the access level of a variable, method or class

Describe the object-oriented programming principle of encapsulation

Identify accessor and mutator methods

Page 69: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Packages and Access Modifiers

Packages

-import statement

Access modifiers

-public-protected-package-private

Page 70: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java 2 Application Programming Interface

Organized into packages such as

-java.lang-java.awt-java.io-java.util-java.net

Page 71: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Encapsulation

Accessor

- Method that reads a variable

Mutator

- Wrapping variables and methods together

Page 72: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

ObjectEncapsulation

Data 1Data 2Data 3Data 4

Page 73: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Compile and run a class with packages

Identify the packages in the Java 2 API

Identify the various access levels in Java

Describe how access modifiers can be used to set the access level of a variable, method or class

Describe the object-oriented programming principle of encapsulation

Identify accessor and mutator methods

Page 74: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 11:Swing

Components

Page 75: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Distinguish between the AWT and Swing

Identify the general organization of the Swing class structure

Define and use Swing widgets and containers

Page 76: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is the AWT?

Heavyweight components

- Peer components

AWT 1.0

- Container propagation

- Single-method event-handling

AWT 1.1

- New event model

Page 77: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is Swing?

Lightweight components

- No peer components

- All Java

Customizable

Model View Controller (MVC) programming paradigm

Included in Java 2 (JDK 1.2)

Page 78: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Basic Swing Components

JComponent

JLabel

JButton

JTextField

JTextArea

JScrollBar

ImageIcon

JScrollPane

JPanel

JFrame

JFileChooser

JApplet

Page 79: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Graphical Widgets

The graphical components with which users interact

-JButton-JLabel-JScrollBar

Page 80: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

SwingContainers

Two types

- Top level (includes JFrame, Window)

- Lower level (includes JPanel, JScrollPane)

Page 81: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Distinguish between the AWT and Swing

Identify the general organization of the Swing class structure

Define and use Swing widgets and containers

Page 82: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 12:Layout Managers

Page 83: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define a layout manager

Set a layout manager for a Container

Effectively use FlowLayout, GridLayout, BorderLayout and BoxLayout

Nest containers and layout managers to form more complex GUI layouts

Separate a complex design into its component containers and layout managers

Page 84: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is a Layout Manager?

FlowLayout

GridLayout

BorderLayout

BoxLayout

CardLayout

GridBagLayout

GridLayout

OverlayLayout

ScrollPaneLayout

ViewportLayout

Page 85: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

FlowLayout

Centers components on each line in a flowing manner

Page 86: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

GridLayout

Adds components in a gridlike format

Page 87: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

BorderLayout

CENTER

EAST

WEST

SOUTH

NORTH

Page 88: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

BoxLayout

Allows either a horizontal or a vertical layout of components

Page 89: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

CombiningLayouts

Button 1

Button 2

Button 3

Button 4

Page 90: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define a layout manager

Set a layout manager for a Container

Effectively use FlowLayout, GridLayout, BorderLayout and BoxLayout

Nest containers and layout managers to form more complex GUI layouts

Separate a complex design into its component containers and layout managers

Page 91: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 13:Graphicsin Java

Page 92: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Identify the AWT class structure for graphics

Gain access to a container’s graphic context by overriding the paint(Graphics g) method

Use methods of the Graphics class via the graphics context

Effectively use the Color and Font classes

Page 93: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Graphics Class

The graphics context is used to access the functionality of the Graphics class (an abstract class)

Page 94: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Graphics Class Methods

draw3DRect()

drawArc()

drawLine()

drawOval()

drawPolygon()

drawPolyline()

drawRect()

drawRoundRect()

fill3DRect()

fillArc()

fillOval()

fillPolygon()

fillRect()

fillRoundRect()

Page 95: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Other Classes

Color setColor()

Font setFont()-FontMetrics

Page 96: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Identify the AWT class structure for graphics

Gain access to a container’s graphic context by overriding the paint(Graphics g) method

Use methods of the Graphics class via the graphics context

Effectively use the Color and Font classes

Page 97: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 14:The Event

Delegation Model

Page 98: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Describe the event delegation model

Create listener classes that can respond to events

Register listener classes with their Component sources

Capture events and deal with them in meaningful ways

Page 99: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Event?

What caused the event?

What was the exact nature of the event?

Is additional information available about the event?

Page 100: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

JDK 1.0 Event Handling

Tightly coupled with the AWT

Inefficient

General

Unruly

Code and event handling could not be separated

Page 101: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

SDK 1.2 Event Handling

Generating the event object

Sending the event object to the listener

Preparing the listener to receive the event

Example: Creating a closeable JFrame

JFrame convenience methods for event handling

Example: Event handling and callbacks

Page 102: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Describe the event delegation model

Create listener classes that can respond to events

Register listener classes with their Component sources

Capture events and deal with them in meaningful ways

Page 103: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 15:Inner

Classes

Page 104: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define an inner class

Recognize the advantages of inner classes over package-level classes in relation to event handling

Design and implement inner classes for event handling

Page 105: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Inner Class?

A class defined within other classes

Introduced with the SDK 1.2

Page 106: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Inner Classes for Event Handling

Member inner classes

Anonymous inner classes

Page 107: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define an inner class

Recognize the advantages of inner classes over package-level classes in relation to event handling

Design and implement inner classes for event handling

Page 108: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 16:Java

Applets

Page 109: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Compare and contrast Java applets and applications

Implement the life cycle of an applet through its inherited methods

Embed an applet into an HTML document

Pass parameters from an HTML document to its contained applet

Identify applet security restrictions

Convert an applet into an application

Page 110: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

JApplet Class Hierarchy

Object

Component

Applet

Panel

Container

JApplet

Page 111: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Applets and Web Browsers

Applets do not have a main() method

Applets have a life cycle

Applets are started from HTML pages, and receive information from HTML pages

Because they are program code, applets should not be trusted

Page 112: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Converting an Application into an Applet

Use init() method to perform instantiation

Instantiating the applet is unnecessary

Applets must be derived from JApplet

Applet size should be set within the HTML document (the Web browser makes it visible)

Applets must be declared public

Page 113: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Converting an Applet into an Application

By adding an instance of the JApplet to the JFrame and calling its init() and start() methods, the conversion from JApplet to stand-alone JFrame readily takes place

Page 114: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Compare and contrast Java applets and applications

Implement the life cycle of an applet through its inherited methods

Embed an applet into an HTML document

Pass parameters from an HTML document to its contained applet

Identify applet security restrictions

Convert an applet into an application

Page 115: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 17:Exceptions

Page 116: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Differentiate between errors and exceptions

Differentiate between runtime exceptions and explicit exceptions

Propagate an exception using the throws statement

Handle an exception using the try/catch statement

Create and use a user-defined exception

Page 117: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is an Exception?

Exceptions

- Runtime exceptions (unchecked)

- Other exceptions (checked)

Page 118: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Exception Class Hierarchy

Page 119: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

HandlingExceptions

Ignore the Exception

Handle the Exception with a try/catch statement

Throw the Exception to the calling method

Handle the Exception and rethrow it to the calling method

Page 120: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Creating User-Defined Exceptions

Creating the exception

Throwing the exception

Exception handling tips

Page 121: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Exception Handling Tips

Use simple tests instead of try/catch

Choose user-defined exceptions wisely

Use one try/catch block for multiple exceptions

Do something meaningful with caught exceptions

Page 122: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Differentiate between errors and exceptions

Differentiate between runtime exceptions and explicit exceptions

Propagate an exception using the throws statement

Handle an exception using the try/catch statement

Create and use a user-defined exception

Page 123: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 18:Creating Threads

and Thread Methods

Page 124: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define threads

Create and instantiate threads using two different techniques

Control single-thread flow

Define the four thread states and their relationships to thread methods

Page 125: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Are Threads?

Multitasking

Multiprocessing

Multithreading

Page 126: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

How Operating Systems Handle Multitasking

Pre-emptive multitasking

Cooperative multitasking

Page 127: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Types of Threads in Java

Daemon thread

User thread

- Main thread

- Other user threads

Page 128: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

The Main Thread

main thread

user thread user thread

user thread user thread

Page 129: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Creating Threads

Subclassing the Thread class

Implementing the Runnable interface

Which technique?

Page 130: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Thread States

New Thread

Dead

Not RunnableRunnable

stop( )

start( )

stop( )

suspend()

resume( )

yield( )

stop( )

Page 131: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define threads

Create and instantiate threads using two different techniques

Control single-thread flow

Define the four thread states and their relationships to thread methods

Page 132: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 19:Thread

Synchronization

Page 133: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define synchronization in relation to object monitors

Control thread racing using thread synchronization

Convert non-atomic to atomic processes to avoid thread racing

Use sophisticated methods for controlling threads

Stop, suspend and resume threads

Explain thread deadlock

Page 134: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is Thread Synchronization?

Controlling threads in a predictable manner

Page 135: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Thread Racing

Two threads trying to access the same data

Page 136: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Synchronized and the Object Monitor

synchronized keyword

Threads waiting in a queue to obtain an objects monitor

All synchronized methods of an object use a single monitor

Page 137: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Thread Race Condition

Competing for resources

Synchronizing the methods

Atomic processes

Page 138: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Sophisticated Thread Synchronization

Consumer/producer scenario

Page 139: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Stopping, Suspending and Resuming Threads

Stopping a thread

Suspending a thread

Resuming a thread

Page 140: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Deadlocks

ObjectA{ synchronized methodA()}

ObjectB{ synchronized methodB()}

Thread1 Thread2

blocked

blocked

Page 141: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define synchronization in relation to object monitors

Control thread racing using thread synchronization

Convert non-atomic to atomic processes to avoid thread racing

Use sophisticated methods for controlling threads

Stop, suspend and resume threads

Explain thread deadlock

Page 142: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 20:Streams and Serialization

Page 143: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define a stream

Differentiate between byte streams and character streams

Recognize the abstraction of byte streams through the InputStream and OutputStream classes

Recognize the abstraction of character streams through the Reader and Writer classes

Create and use file objects

Page 144: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives (cont’d)

Use System.in and System.out to perform stream operations

Nest streams using wrapper classes to enhance basic stream behavior

Perform file I/O

Define object serialization

Use serialization to save an object to a file

Explain the transient keyword

Page 145: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is a Stream?

A path of information from a source to a destination

Page 146: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

InputStream, OutputStream, Reader and

Writer JDK 1.0 (bytes)

-InputStream-OutputStream

JDK 1.1 (characters)

-Reader-Writer

Page 147: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Files

Instantiating a file object

Working with a file object

- Methods of File- Directories

-FileDialog/FileChooser

Page 148: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Stream Classes of java.io.*

System.in and System.out

Reading bytes from System.in

Converting a byte stream into a character stream

Wrapper streams

File I/O

Page 149: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Serialization

The process of object serialization

- Marking an object for serialization

- Writing the object to file

- Reading the serialized object from a file

Transient variables and security

Page 150: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define a stream

Differentiate between byte streams and character streams

Recognize the abstraction of byte streams through the InputStream and OutputStream classes

Recognize the abstraction of character streams through the Reader and Writer classes

Create and use file objects

Page 151: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary (cont’d)

Use System.in and System.out to perform stream operations

Nest streams using wrapper classes to enhance basic stream behavior

Perform file I/O

Define object serialization

Use serialization to save an object to a file

Explain the transient keyword

Page 152: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Lesson 21:Networking

in Java

Page 153: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Objectives

Define networking, IP addresses, the Domain Name System, and ports

Discuss the socket model and socket-to-socket communication

Explain the client/server model

Write a single-threaded client/server echo system

Write a multithreaded client/server echo system

Page 154: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

What Is Networking?

Computers communicating across distances

Java networking

- TCP/IP

- UDP

Page 155: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Connecting Computers Across the Internet

IP addresses

- Dotted quad

- 32 bits

- IPv6

DNS

Sockets

Well-known ports

Page 156: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Well-Known Ports and Their Protocols

Port Protocol

21 FTP

23 Telnet

25 E-mail

79 Finger

80 HTTP

119 NNTP/Usenet

Page 157: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Networking Classes of java.net.*

Common networking classes

-InetAddress-Socket-ServerSocket

Page 158: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

The Java Client/Server Model

Server

- Delivers information

-ServerSocket object

-accept method returns a socket

Client

- Requests information from the server

- Connects to server with socket

Page 159: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Building the EchoServer

The client

- Step 1: getConnection- Step 2: sendMessage- Step 3: receiveMessage

The server

- Step 1: getConnection- Step 2: receiveMessage- Step 3: sendMessage

Page 160: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

MultithreadingYour Client/Server Model

Modifications to the server

The Connection class

The Client class

Running the threaded client/server example

Page 161: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Summary

Define networking, IP addresses, the Domain Name System, and ports

Discuss the socket model and socket-to-socket communication

Explain the client/server model

Write a single-threaded client/server echo system

Write a multithreaded client/server echo system

Page 162: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Programming Fundamentals

Java Runtime Environment

Data Types, Variables and Operators

Control Statements

Methods

Arrays

Classes and Objects

Inheritance

Constructors

Page 163: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Programming Fundamentals (cont’d)

Interfaces and Abstract Classes

Packages and Access Modifiers

Swing Components

Layout Managers

Graphics in Java

The Event Delegation Model

Inner Classes

Page 164: Java Programming Fundamentals. Lesson 1: Java Runtime Environment

Java Applets

Exceptions

Creating Threads and Thread Methods

Thread Synchronization

Streams and Serialization

Networking in Java

Java Programming Fundamentals (cont’d)