Internet Programming Chapter 2
Internet Programming Chapter 2
Overview of Java
Language
Topics of the Review
2
Some Characteristics of Java
3
Java categories / editions
4
Java Processing and Execution
5
Compiling and Executing a Java Program
6
Classes and Objects
8
A Little Example of import and main
import javax.swing.*;
// all classes from javax.swing
public class HelloWorld { // starts a class
public static void main (String[] args) {
// starts a main method
// in: array of String; out: none (void)
}
}
• public = can be seen from any package
• static = not “part of” an object
9
Processing and Running HelloWorld
• javac HelloWorld.java
• Produces HelloWorld.class (byte code)
• java HelloWorld
• Starts the JVM and runs the main method
10
References and Primitive Data Types
11
Primitive Data Types
12
Primitive Data Types
13
Primitive Data Types (continued)
14
Operators
15
Operators
16
Type Compatibility and Conversion
• Widening conversion:
• In operations on mixed-type operands, the numeric
type of the smaller range is converted to the numeric
type of the larger range
• In an assignment, a numeric type of smaller range
can be assigned to a numeric type of larger range
• byte to short to int to long
• int kind to float to double
17
Declaring and Setting Variables
• int square;
square = n * n;
• double cube = n * (double)square;
• Can generally declare local variables where they are
initialized
• All variables get a safe initial value anyway (zero/null)
18
Referencing and Creating Objects
19
Java Control Statements
20
Java Control Statements (continued)
21
Java Control Statements (continued)
22
Methods
23
Escape Sequences
24
The String Class
25
Comparing Objects
26
The StringBuffer Class
27
StringTokenizer Class
28
Wrapper Classes for Primitive Types
29
Defining Your Own Classes
Field
signatures:
type and name
Method signatures:
name, argument Field Class
types, result type Class values name
name
30
Defining Your Own Classes (continued)
31
The Person Class
34
The Person Class (4)
35
The Person Class (5)
// “printing” a Person
public String toString () {
return “Given name: “ + givenName + “\n”
+ “Family name: “ + familyName + “\n”
+ “ID number: “ + IDNumber + “\n”
+ “Year of birth: “ + birthYear + “\
n”;
}
36
The Person Class (6)
// same Person?
public boolean equals (Person per) {
return (per == null) ? false :
this.IDNumber.equals(per.IDNumber);
}
37
Arrays
38
Array Example
39
Array Example Variations
40
Input/Output using Class JOptionPane
41
Input/Output using Class JOptionPane
(continued)
42
Converting Numeric Strings to Numbers
43
Summary of the Review
44