Download Chapter Four - Elements of Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
George Blank
University Lecturer
CS 602
Java and the Web
Object Oriented Software Development
Using Java
Chapter 4
Elements of Java
• This chapter is a catalog of the Java
language. It contains a lot of information in a
highly compressed format. Some students
may wish to use another supplementary Java
textbook that has a lot of exercises to practice
different features of the language instead of
having so many at once. There are lots of
such texts in bookstores.
Java Uniqueness
•
•
•
•
•
•
•
•
Purely object-oriented—no compromises!
Primitive types are not objects
No multiple inheritance (simulate with interfaces)
No GOTO statement
16 bit international characters only (Unicode)
Exception handling
Automatic memory management (garbage collection)
Multithreaded programming.
Standardization
• In many computer languages, there are
differences between implementations of the
language on different platforms or with
different compilers. For example, C long
integers might have different maximum and
minimum values. It is a strength of Java that
everything is standardized on all platforms.
Variables and Types
• A variable is a location in memory where values can
be stored and referenced.
• Variables are associated with types, which have
particular data sizes and a restricted set of valid
operations that can be performed on them.
• Variable declarations assign an identifier, a type, and
may declare an initial value. Many types have a
default value. For example, an uninitialized integer
defaults to 0.
Variable (a memory location)
a value
(a default value)
a type
address
a name
The address or value may be passed as a parameter
Constructors
• A constructor is a special form of method that has only
one purpose—to create an instance of a class. This is
different from other methods, which add behavior to a
class.
• It is good Java practice to supply a no-arguments
constructor in addition to any that take parameters to
allow the Java Virtual Machine to create instances of the
class dynamically at run time. The text shows two
constructors for Point (next slide).
Point example (text, p. 104)
public class Point {
public double x, y;
public Point() { //no-arg constructor
x = 0.0; y = 0.0;
}
public Point(double init_x, double init_y) {
x = init_x; y = init_y;
}
…
}
• Here, the no-arg constructor supplies default values for the
point location.
Singleton Classes
• Classes that are not supposed to have more
than one instance at a time, such as a toplevel window or a system timer, can be limited
as a singleton class, an instance of the
Singleton design pattern that will be
discussed in a future lecture.
• The following slide shows a singleton class.
Singleton Class
Note the private static constructor that prevents multiple instances.
Reference Types
• References to classes, interfaces and arrays
are implemented in Java as 32 bit pointers
and are called reference types.
• While C and C++ pointers can be cast to
other types, that is forbidden in Java. No
direct manipulation of reference types is
permitted. This reduces errors and increases
security.
Variable Scope
• Local variables are only valid within the block
in which they are declared. Blocks in Java
open with { and close with } and can have
other blocks embedded in them. Local
variables are not automatically initialized.
Variable declarations must be terminated with
a;
Flow Control
•
•
•
•
return ends a method and returns control to the caller
if and else are used for selection
while, do-while and for are used for loops
break and continue expand flow control
Class Declarations
• Class declarations define classes, which can
be instantiated as objects and are assigned
to a reference type.
• A class declaration includes a class name
and fields, methods, and nested class
declarations. The fields are known as the
variables or attributes of a class.
Class and Method Modifiers
•
•
•
•
•
<none> –Field is accessible by all others within a package
public –Field is accessible by any class
private –Field is accessible only by itself
static –Field is shared by all instances of a class
abstract –Class contains only abstract methods, method
defers implementation to subclasses
• final –Field may not be extended with subclasses
A field may be a class or method.
Specialized Method Modifiers
• synchronized – an atomic thread in a
multithread environment
• native –implemented in C and C++
main Method
• Every program must have a main method, declared
as
public static void main(String [] args) {
…
}
• The array of String arguments allows initialization
values to be passed to the class at start up time. Only
one class in the program requires a main method.
Print Command args (NetBeans)
Things to understand
• Here are some of the features of Java that
students have had trouble with in the past,
with page numbers from the text:
• String concatenation p. 84
• Assignment p. 87
• Casts p. 89
• Initialization p. 91
More things to understand
•
•
•
•
•
•
•
Multidimensional arrays p. 93
Expressions for object creation p. 94
Local variables and scope p. 95
The return statement p. 95
Break and continue p. 99
Class and method modifiers pp. 101-102
Accessibility p. 103
More things to understand
•
•
•
•
•
•
•
Constructors p. 104
Accessors p. 106
Parameter Passing pp. 107-109
Class fields pp. 110-111
Singletons p. 114
This pp.114-115
Interfaces pp. 117-118
More things to understand
•
•
•
•
•
•
String methods p. 119
Standard input, output and error pp. 123ff
Wrappers pp. 128-129
Packages pp. 134-136
Exceptions pp. 139-148
The chapter summary contains many
references to key concepts.
Code Examples from text
• Note: All the code that follows contains the
following header which has been removed to
fit on the screen:
• /*
• * Copyright (c) 1999-2002, Xiaoping Jia.
• * All Rights Reserved.
• */
• Respect the author’s copyright!
Break
Records
Bubble
Sort
Copy
Copy Text File
Digital
Clock
page 1
Digital
Clock
page 2
MaxMin
Maximum (with try-catch)
String
Compare
An Interned String
is a copy of a String
that has the same
content but is a
different String.
Word:
String
Tokenizer
String Handling
• Understand the difference between the
immutable String class and the mutable
StringBuffer class.
• Strings have special privileges not shared by
ordinary classes including the creation of
string literals and the use of concatenation
operators.
Garbage collection
• Garbage collection, especially remote
garbage collection, is a big deal in Java. See
the explanation on page 90.
• One reason that garbage collection is so
important is that C does not have it, and a
substantial number of problems in Microsoft
Windows come from the failure to free
resources no longer in use.
Bibliography
• Jia, Xiaoping, Object Oriented Software
Development Using Java. Addison Wesley,
2003