Contents [0/53] |
Object-Oriented Design & Patterns [1/53] |
Chapter Topics [2/53] |
"Hello, World" in Java [3/53] |
file:horstmann/ch01_helloworld/Greeter.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package horstmann.ch01_helloworld; /** A class for producing simple greetings. */ public class Greeter { /** Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. */ public Greeter(String aName) { name = aName; } /** Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. */ public String sayHello() { return "Hello, " + name + "!"; } private String name; }
"Hello, World" in Java [4/53] |
new Greeter("World")
new Greeter("World").sayHello()
Greeter worldGreeter = new Greeter("World");
String greeting = worldGreeter.sayHello();
"Hello, World" in Java [5/53] |
file:horstmann/ch01_helloworld/GreeterTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
package horstmann.ch01_helloworld; public class GreeterTester { public static void main(String[] args) { Greeter worldGreeter = new Greeter("World"); String greeting = worldGreeter.sayHello(); System.out.println(greeting); } }
file:horstmann/ch01_helloworld/Greeter.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package horstmann.ch01_helloworld; /** A class for producing simple greetings. */ public class Greeter { /** Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. */ public Greeter(String aName) { name = aName; } /** Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. */ public String sayHello() { return "Hello, " + name + "!"; } private String name; }
Using the SDK [6/53] |
javac GreeterTest.javaNote that Greeter.java is automatically compiled.
java GreeterTest
Using the SDK [7/53] |
Using BlueJ [8/53] |
Using BlueJ [9/53] |
Documentation Comments [10/53] |
Documentation Comments - Summary [11/53] |
Documentation Comments - Detail [12/53] |
Documentation Comments [13/53] |
Documentation Comments - API Docs [14/53] |
Primitive Types [15/53] |
y = Math.sqrt(x);
Control Flow [16/53] |
for (int i = 1; i <= n; i++)
{
. . .
}
// i no longer defined here
Object References [17/53] |
Greeter worldGreeter = new Greeter("World");
Greeter anotherGreeter = worldGreeter;
anotherGreeter.setName("Dave");
// now worldGreeter.sayHello() returns "Hello, Dave!"
The null Reference [18/53] |
worldGreeter = null;
if (worldGreeter == null) . . .
The this Reference [19/53] |
public boolean equals(Greeter other)
{
if (this == other) return true;
return name.equals(other.name);
}
public Greeter(String name)
{
this.name = name;
}
Parameter Passing [20/53] |
public void copyNameTo(Greeter other)
{
other.name = this.name;
}
Greeter worldGreeter = new Greeter("World");
Greeter daveGreeter = new Greeter("Dave");
worldGreeter.copyNameTo(daveGreeter);
No Reference Parameters [21/53] |
public void copyLengthTo(int n)
{
n = name.length();
}
public void copyGreeterTo(Greeter other)
{
other = new Greeter(name);
}
int length = 0;
worldGreeter.copyLengthTo(length); // length still 0
worldGreeter.copyGreeterTo(daveGreeter) // daveGreeter unchanged
Packages [22/53] |
java.util
javax.swing
com.sun.misc
edu.sjsu.cs.cs151.alice
Packages [23/53] |
package edu.sjsu.cs.cs151.alice;
public class Greeter { . . . }
java.util.ArrayList
javax.swing.JOptionPane
Importing Packages [24/53] |
import java.util.Scanner;
. . .
Scanner a; // i.e. java.util.Scanner
import java.util.*;
Importing Packages [25/53] |
import java.*.*; // NO
import java.util.*;
import java.sql.*;
. . .
java.util.Date d; // Date also occurs in java.sql
Packages and Directories [26/53] |
edu.sjsu.cs.sjsu.cs151.alice.Greetermust be in subdirectory
basedirectory/edu/sjsu/cs/sjsu/cs151/alice
javac edu/sjsu/cs/sjsu/cs151/alice/Greeter.javaor
javac edu\sjsu\cs\sjsu\cs151\alice\Greeter.java
java edu.sjsu.cs.cs151.alice.GreeterTest
Exception Handling [27/53] |
String name = null;
int n = name.length(); // ERROR
Exception in thread "main" java.lang.NullPointerException
at Greeter.sayHello(Greeter.java:25)
at GreeterTest.main(GreeterTest.java:6)
Checked and Unchecked Exceptions [28/53] |
Declaring Checked Exceptions [29/53] |
public void read(String filename) throws FileNotFoundException
{
FileReader reader = new FileReader(filename);
. . .
}
public void read(String filename)
throws IOException, ClassNotFoundException
public static void main(String[] args)
throws IOException, ClassNotFoundException
Catching Exceptions [30/53] |
try
{
code that might throw an IOException
}
catch (IOException exception)
{
take corrective action
}
exception.printStackTrace();
System.exit(1);
The finally Clause [31/53] |
FileReader reader = null;
try
{
reader = new FileReader(name);
...
}
finally
{
if (reader != null) reader.close();
}
Strings [32/53] |
Strings [33/53] |
String concatenation [34/53] |
"Hello, " + name
int n = 7;
String greeting = "Hello, " + n;
// yields "Hello, 7"
Date now = new Date();
String greeting = "Hello, " + now;
// concatenates now.toString()
// yields "Hello, Wed Jan 17 16:57:18 PST 2001"
Converting Strings to Numbers [35/53] |
String input = "7";
int n = Integer.parseInt(input);
// yields integer 7
Reading Input [36/53] |
file:horstmann/ch01_input/InputTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package horstmann.ch01_input; import java.util.Scanner; public class InputTester { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("How old are you?"); int age = in.nextInt(); age++; System.out.println("Next year, you'll be " + age); in.close(); } }
The ArrayList class [37/53] |
ArrayList<String> countries = new ArrayList<String>();
countries.add("Belgium");
countries.add("Italy");
countries.add("Thailand");
The ArrayList class [38/53] |
for (String country : countries) . . .
The ArrayList class [39/53] |
Linked Lists [40/53] |
LinkedList<String> countries = new LinkedList<String>();
countries.add("Belgium");
countries.add("Italy");
countries.add("Thailand");
List Iterators [41/53] |
List Iterators [42/53] |
Arrays [43/53] |
Arrays [44/53] |
for (int i = 0; i < numbers.length; i++)
Arrays [45/53] |
numbers = new int[0];
int[][] table = new int[10][20];
int t = table[i][j];
Command-Line Arguments [46/53] |
Static Fields [47/53] |
public class Greeter
{
. . .
private static Random generator;
}
public class Math
{
. . .
public static final double PI = 3.14159265358979323846;
}
Static Methods [48/53] |
public static Greeter getRandomInstance()
{
if (generator.nextBoolean()) // note: generator is static field
return new Greeter("Mars");
else
return new Greeter("Venus");
}
Greeter g = Greeter.getRandomInstance();
Programming Style: Case Convention [49/53] |
name
sayHello
Greeter
ArrayList
PI
MAX_VALUE
Programming Style: Property Access [50/53] |
String getName()
void setName(String newValue)
public boolean isPolite()
public void setPolite(boolean newValue)
Programming Style: Braces [51/53] |
public String sayHello()
{
return "Hello, " + name + "!";
}
public String sayHello() {
return "Hello, " + name + "!";
}
Programming Style: Fields [52/53] |
public class Greeter
{
private String name;
public Greeter(String aName) { . . . }
. . .
}
Programming Style: Miscellaneous [53/53] |
Good: if (x > Math.sqrt(y))
Bad: if(x>Math.sqrt (y))
Good: int[] numbers
Bad: int numbers[]
Good: h = HASH_MULTIPLIER * h + val[off];
Bad: h = 31 * h + val[off];
Revised: 2007/09/11 16:59