{
5 | public final A first;
6 | public final B second;
7 | public TwoTuple(A a, B b) { first = a; second = b; }
8 | public String toString() {
9 | return "(" + first + ", " + second + ")";
10 | }
11 | } ///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/object/Documentation1.java:
--------------------------------------------------------------------------------
1 | package object;
2 |
3 | /** A class comment */
4 | public class Documentation1 {
5 | /** A field comment */
6 | public int i;
7 | /** A method comment */
8 | public void f() {}
9 | } ///:~
10 |
--------------------------------------------------------------------------------
/src/main/java/object/Documentation2.java:
--------------------------------------------------------------------------------
1 | package object;
2 |
3 | /**
4 | *
5 | * System.out.println(new Date());
6 | *
7 | */
8 | public class Documentation2 {}
9 | ///:~
10 |
--------------------------------------------------------------------------------
/src/main/java/object/Documentation3.java:
--------------------------------------------------------------------------------
1 | package object;
2 |
3 | /**
4 | * You can even insert a list:
5 | *
6 | * - Item one
7 | *
- Item two
8 | *
- Item three
9 | *
10 | */
11 | public class Documentation3 {}
12 | ///:~
13 |
--------------------------------------------------------------------------------
/src/main/java/object/HelloDate.java:
--------------------------------------------------------------------------------
1 | package object;
2 | import java.util.*;
3 |
4 | /** The first Thinking in Java example program.
5 | * Displays a string and today's date.
6 | * @author Bruce Eckel
7 | * @author www.MindView.net
8 | * @version 4.0
9 | */
10 | public class HelloDate {
11 | /** Entry point to class & application.
12 | * @param args array of string arguments
13 | * @throws exceptions No exceptions thrown
14 | */
15 | public static void main(String[] args) {
16 | System.out.println("Hello, it's: ");
17 | System.out.println(new Date());
18 | }
19 | } /* Output: (55% match)
20 | Hello, it's:
21 | Wed Oct 05 14:39:36 MDT 2005
22 | *///:~
23 |
--------------------------------------------------------------------------------
/src/main/java/object/ShowProperties.java:
--------------------------------------------------------------------------------
1 | package object;
2 |
3 | public class ShowProperties {
4 | public static void main(String[] args) {
5 | System.getProperties().list(System.out);
6 | System.out.println(System.getProperty("user.name"));
7 | System.out.println(
8 | System.getProperty("java.library.path"));
9 | }
10 | } ///:~
11 |
--------------------------------------------------------------------------------
/src/main/java/operators/AutoInc.java:
--------------------------------------------------------------------------------
1 | //: operators/AutoInc.java
2 | // Demonstrates the ++ and -- operators.
3 | package operators;
4 | import static net.mindview.util.Print.*;
5 |
6 | public class AutoInc {
7 | public static void main(String[] args) {
8 | int i = 1;
9 | print("i : " + i);
10 | print("++i : " + ++i); // Pre-increment
11 | print("i++ : " + i++); // Post-increment
12 | print("i : " + i);
13 | print("--i : " + --i); // Pre-decrement
14 | print("i-- : " + i--); // Post-decrement
15 | print("i : " + i);
16 | }
17 | } /* Output:
18 | i : 1
19 | ++i : 2
20 | i++ : 2
21 | i : 3
22 | --i : 2
23 | i-- : 2
24 | i : 1
25 | *///:~
26 |
--------------------------------------------------------------------------------
/src/main/java/operators/Casting.java:
--------------------------------------------------------------------------------
1 | //: operators/Casting.java
2 | package operators;
3 | public class Casting {
4 | public static void main(String[] args) {
5 | int i = 200;
6 | long lng = (long)i;
7 | lng = i; // "Widening," so cast not really required
8 | long lng2 = (long)200;
9 | lng2 = 200;
10 | // A "narrowing conversion":
11 | i = (int)lng2; // Cast required
12 | }
13 | } ///:~
14 |
--------------------------------------------------------------------------------
/src/main/java/operators/CastingNumbers.java:
--------------------------------------------------------------------------------
1 | //: operators/CastingNumbers.java
2 | // What happens when you cast a float
3 | // or double to an integral value?
4 | package operators;
5 | import static net.mindview.util.Print.*;
6 |
7 | public class CastingNumbers {
8 | public static void main(String[] args) {
9 | double above = 0.7, below = 0.4;
10 | float fabove = 0.7f, fbelow = 0.4f;
11 | print("(int)above: " + (int)above);
12 | print("(int)below: " + (int)below);
13 | print("(int)fabove: " + (int)fabove);
14 | print("(int)fbelow: " + (int)fbelow);
15 | }
16 | } /* Output:
17 | (int)above: 0
18 | (int)below: 0
19 | (int)fabove: 0
20 | (int)fbelow: 0
21 | *///:~
22 |
--------------------------------------------------------------------------------
/src/main/java/operators/EqualsMethod.java:
--------------------------------------------------------------------------------
1 | //: operators/EqualsMethod.java
2 | package operators;
3 | public class EqualsMethod {
4 | public static void main(String[] args) {
5 | Integer n1 = new Integer(47);
6 | Integer n2 = new Integer(47);
7 | System.out.println(n1.equals(n2));
8 | }
9 | } /* Output:
10 | true
11 | *///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/operators/EqualsMethod2.java:
--------------------------------------------------------------------------------
1 | //: operators/EqualsMethod2.java
2 | // Default equals() does not compare contents.
3 | package operators;
4 | class Value {
5 | int i;
6 | }
7 |
8 | public class EqualsMethod2 {
9 | public static void main(String[] args) {
10 | Value v1 = new Value();
11 | Value v2 = new Value();
12 | v1.i = v2.i = 100;
13 | System.out.println(v1.equals(v2));
14 | }
15 | } /* Output:
16 | false
17 | *///:~
18 |
--------------------------------------------------------------------------------
/src/main/java/operators/Equivalence.java:
--------------------------------------------------------------------------------
1 | //: operators/Equivalence.java
2 | package operators;
3 | public class Equivalence {
4 | public static void main(String[] args) {
5 | Integer n1 = new Integer(47);
6 | Integer n2 = new Integer(47);
7 | System.out.println(n1 == n2);
8 | System.out.println(n1 != n2);
9 | }
10 | } /* Output:
11 | false
12 | true
13 | *///:~
14 |
--------------------------------------------------------------------------------
/src/main/java/operators/Exponents.java:
--------------------------------------------------------------------------------
1 | //: operators/Exponents.java
2 | // "e" means "10 to the power."
3 | package operators;
4 | public class Exponents {
5 | public static void main(String[] args) {
6 | // Uppercase and lowercase 'e' are the same:
7 | float expFloat = 1.39e-43f;
8 | expFloat = 1.39E-43f;
9 | System.out.println(expFloat);
10 | double expDouble = 47e47d; // 'd' is optional
11 | double expDouble2 = 47e47; // Automatically double
12 | System.out.println(expDouble);
13 | }
14 | } /* Output:
15 | 1.39E-43
16 | 4.7E48
17 | *///:~
18 |
--------------------------------------------------------------------------------
/src/main/java/operators/HelloDate.java:
--------------------------------------------------------------------------------
1 | //: operators/HelloDate.java
2 | package operators;
3 | import java.util.*;
4 | import static net.mindview.util.Print.*;
5 |
6 | public class HelloDate {
7 | public static void main(String[] args) {
8 | print("Hello, it's: ");
9 | print(new Date());
10 | }
11 | } /* Output: (55% match)
12 | Hello, it's:
13 | Wed Oct 05 14:39:05 MDT 2005
14 | *///:~
15 |
--------------------------------------------------------------------------------
/src/main/java/operators/Overflow.java:
--------------------------------------------------------------------------------
1 | //: operators/Overflow.java
2 | // Surprise! Java lets you overflow.
3 | package operators;
4 | public class Overflow {
5 | public static void main(String[] args) {
6 | int big = Integer.MAX_VALUE;
7 | System.out.println("big = " + big);
8 | int bigger = big * 4;
9 | System.out.println("bigger = " + bigger);
10 | }
11 | } /* Output:
12 | big = 2147483647
13 | bigger = -4
14 | *///:~
15 |
--------------------------------------------------------------------------------
/src/main/java/operators/PassObject.java:
--------------------------------------------------------------------------------
1 | //: operators/PassObject.java
2 | // Passing objects to methods may not be
3 | // what you're used to.
4 | package operators;
5 | import static net.mindview.util.Print.*;
6 |
7 | class Letter {
8 | char c;
9 | }
10 |
11 | public class PassObject {
12 | static void f(Letter y) {
13 | y.c = 'z';
14 | }
15 | public static void main(String[] args) {
16 | Letter x = new Letter();
17 | x.c = 'a';
18 | print("1: x.c: " + x.c);
19 | f(x);
20 | print("2: x.c: " + x.c);
21 | }
22 | } /* Output:
23 | 1: x.c: a
24 | 2: x.c: z
25 | *///:~
26 |
--------------------------------------------------------------------------------
/src/main/java/operators/Precedence.java:
--------------------------------------------------------------------------------
1 | //: operators/Precedence.java
2 | package operators;
3 | public class Precedence {
4 | public static void main(String[] args) {
5 | int x = 1, y = 2, z = 3;
6 | int a = x + y - 2/2 + z; // (1)
7 | int b = x + (y - 2)/(2 + z); // (2)
8 | System.out.println("a = " + a + " b = " + b);
9 | }
10 | } /* Output:
11 | a = 5 b = 1
12 | *///:~
13 |
--------------------------------------------------------------------------------
/src/main/java/operators/RoundingNumbers.java:
--------------------------------------------------------------------------------
1 | //: operators/RoundingNumbers.java
2 | // Rounding floats and doubles.
3 | package operators;
4 | import static net.mindview.util.Print.*;
5 |
6 | public class RoundingNumbers {
7 | public static void main(String[] args) {
8 | double above = 0.7, below = 0.4;
9 | float fabove = 0.7f, fbelow = 0.4f;
10 | print("Math.round(above): " + Math.round(above));
11 | print("Math.round(below): " + Math.round(below));
12 | print("Math.round(fabove): " + Math.round(fabove));
13 | print("Math.round(fbelow): " + Math.round(fbelow));
14 | }
15 | } /* Output:
16 | Math.round(above): 1
17 | Math.round(below): 0
18 | Math.round(fabove): 1
19 | Math.round(fbelow): 0
20 | *///:~
21 |
--------------------------------------------------------------------------------
/src/main/java/operators/StringOperators.java:
--------------------------------------------------------------------------------
1 | //: operators/StringOperators.java
2 | package operators;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class StringOperators {
6 | public static void main(String[] args) {
7 | int x = 0, y = 1, z = 2;
8 | String s = "x, y, z ";
9 | print(s + x + y + z);
10 | print(x + " " + s); // Converts x to a String
11 | s += "(summed) = "; // Concatenation operator
12 | print(s + (x + y + z));
13 | print("" + x); // Shorthand for Integer.toString()
14 | }
15 | } /* Output:
16 | x, y, z 012
17 | 0 x, y, z
18 | x, y, z (summed) = 3
19 | 0
20 | *///:~
21 |
--------------------------------------------------------------------------------
/src/main/java/operators/TernaryIfElse.java:
--------------------------------------------------------------------------------
1 | //: operators/TernaryIfElse.java
2 | package operators;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class TernaryIfElse {
6 | static int ternary(int i) {
7 | return i < 10 ? i * 100 : i * 10;
8 | }
9 | static int standardIfElse(int i) {
10 | if(i < 10)
11 | return i * 100;
12 | else
13 | return i * 10;
14 | }
15 | public static void main(String[] args) {
16 | print(ternary(9));
17 | print(ternary(10));
18 | print(standardIfElse(9));
19 | print(standardIfElse(10));
20 | }
21 | } /* Output:
22 | 900
23 | 100
24 | 900
25 | 100
26 | *///:~
27 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/CovariantReturn.java:
--------------------------------------------------------------------------------
1 | package polymorphism;
2 |
3 | class Grain {
4 | public String toString() { return "Grain"; }
5 | }
6 |
7 | class Wheat extends Grain {
8 | public String toString() { return "Wheat"; }
9 | }
10 |
11 | class Mill {
12 | Grain process() { return new Grain(); }
13 | }
14 |
15 | class WheatMill extends Mill {
16 | Wheat process() { return new Wheat(); }
17 | }
18 |
19 | public class CovariantReturn {
20 | public static void main(String[] args) {
21 | Mill m = new Mill();
22 | Grain g = m.process();
23 | System.out.println(g);
24 | m = new WheatMill();
25 | g = m.process();
26 | System.out.println(g);
27 | }
28 | } /* Output:
29 | Grain
30 | Wheat
31 | *///:~
32 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/PrivateOverride.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/PrivateOverride.java
2 | // Trying to override a private method.
3 | package polymorphism;
4 | import static net.mindview.util.Print.*;
5 |
6 | public class PrivateOverride {
7 | private void f() { print("private f()"); }
8 | public static void main(String[] args) {
9 | PrivateOverride po = new Derived();
10 | po.f();
11 | }
12 | }
13 |
14 | class Derived extends PrivateOverride {
15 | public void f() { print("public f()"); }
16 | } /* Output:
17 | private f()
18 | *///:~
19 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/Shapes.java:
--------------------------------------------------------------------------------
1 | package polymorphism;
2 | // Polymorphism in Java.
3 | import polymorphism.shape.*;
4 |
5 | public class Shapes {
6 | private static RandomShapeGenerator gen =
7 | new RandomShapeGenerator();
8 | public static void main(String[] args) {
9 | Shape[] s = new Shape[9];
10 | // Fill up the array with shapes:
11 | for(int i = 0; i < s.length; i++)
12 | s[i] = gen.next();
13 | // Make polymorphic method calls:
14 | for(Shape shp : s)
15 | shp.draw();
16 | }
17 | } /* Output:
18 | Triangle.draw()
19 | Triangle.draw()
20 | Square.draw()
21 | Triangle.draw()
22 | Square.draw()
23 | Triangle.draw()
24 | Square.draw()
25 | Triangle.draw()
26 | Circle.draw()
27 | *///:~
28 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/music/Instrument.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/music/Instrument.java
2 | package polymorphism.music;
3 | import static net.mindview.util.Print.*;
4 |
5 | class Instrument {
6 | public void play(Note n) {
7 | print("Instrument.play()");
8 | }
9 | }
10 | ///:~
11 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/music/Music.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/music/Music.java
2 | // Inheritance & upcasting.
3 | package polymorphism.music;
4 |
5 | public class Music {
6 | public static void tune(Instrument i) {
7 | // ...
8 | i.play(Note.MIDDLE_C);
9 | }
10 | public static void main(String[] args) {
11 | Wind flute = new Wind();
12 | tune(flute); // Upcasting
13 | }
14 | } /* Output:
15 | Wind.play() MIDDLE_C
16 | *///:~
17 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/music/Note.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/music/Note.java
2 | // Notes to play on musical instruments.
3 | package polymorphism.music;
4 |
5 | public enum Note {
6 | MIDDLE_C, C_SHARP, B_FLAT; // Etc.
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/music/Wind.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/music/Wind.java
2 | package polymorphism.music;
3 |
4 | // Wind objects are instruments
5 | // because they have the same interface:
6 | public class Wind extends Instrument {
7 | // Redefine interface method:
8 | public void play(Note n) {
9 | System.out.println("Wind.play() " + n);
10 | }
11 | } ///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/shape/Circle.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/shape/Circle.java
2 | package polymorphism.shape;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class Circle extends Shape {
6 | public void draw() { print("Circle.draw()"); }
7 | public void erase() { print("Circle.erase()"); }
8 | } ///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/shape/RandomShapeGenerator.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/shape/RandomShapeGenerator.java
2 | // A "factory" that randomly creates shapes.
3 | package polymorphism.shape;
4 | import java.util.*;
5 |
6 | public class RandomShapeGenerator {
7 | private Random rand = new Random(47);
8 | public Shape next() {
9 | switch(rand.nextInt(3)) {
10 | default:
11 | case 0: return new Circle();
12 | case 1: return new Square();
13 | case 2: return new Triangle();
14 | }
15 | }
16 | } ///:~
17 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/shape/Shape.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/shape/Shape.java
2 | package polymorphism.shape;
3 |
4 | public class Shape {
5 | public void draw() {}
6 | public void erase() {}
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/shape/Square.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/shape/Square.java
2 | package polymorphism.shape;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class Square extends Shape {
6 | public void draw() { print("Square.draw()"); }
7 | public void erase() { print("Square.erase()"); }
8 | } ///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/polymorphism/shape/Triangle.java:
--------------------------------------------------------------------------------
1 | //: polymorphism/shape/Triangle.java
2 | package polymorphism.shape;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class Triangle extends Shape {
6 | public void draw() { print("Triangle.draw()"); }
7 | public void erase() { print("Triangle.erase()"); }
8 | } ///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/reusing/Cartoon.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // Constructor calls during inheritance.
3 | import static net.mindview.util.Print.*;
4 |
5 | class Art {
6 | Art() { print("Art constructor"); }
7 | }
8 |
9 | class Drawing extends Art {
10 | Drawing() { print("Drawing constructor"); }
11 | }
12 |
13 | public class Cartoon extends Drawing {
14 | public Cartoon() { print("Cartoon constructor"); }
15 | public static void main(String[] args) {
16 | Cartoon x = new Cartoon();
17 | }
18 | } /* Output:
19 | Art constructor
20 | Drawing constructor
21 | Cartoon constructor
22 | *///:~
23 |
--------------------------------------------------------------------------------
/src/main/java/reusing/Chess.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // Inheritance, constructors and arguments.
3 | import static net.mindview.util.Print.*;
4 |
5 | class Game {
6 | Game(int i) {
7 | print("Game constructor");
8 | }
9 | }
10 |
11 | class BoardGame extends Game {
12 | BoardGame(int i) {
13 | super(i);
14 | print("BoardGame constructor");
15 | }
16 | }
17 |
18 | public class Chess extends BoardGame {
19 | Chess() {
20 | super(11);
21 | print("Chess constructor");
22 | }
23 | public static void main(String[] args) {
24 | Chess x = new Chess();
25 | }
26 | } /* Output:
27 | Game constructor
28 | BoardGame constructor
29 | Chess constructor
30 | *///:~
31 |
--------------------------------------------------------------------------------
/src/main/java/reusing/FinalArguments.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // Using "final" with method arguments.
3 |
4 | class Gizmo {
5 | public void spin() {}
6 | }
7 |
8 | public class FinalArguments {
9 | void with(final Gizmo g) {
10 | //! g = new Gizmo(); // Illegal -- g is final
11 | }
12 | void without(Gizmo g) {
13 | g = new Gizmo(); // OK -- g not final
14 | g.spin();
15 | }
16 | // void f(final int i) { i++; } // Can't change
17 | // You can only read from a final primitive:
18 | int g(final int i) { return i + 1; }
19 | public static void main(String[] args) {
20 | FinalArguments bf = new FinalArguments();
21 | bf.without(null);
22 | bf.with(null);
23 | }
24 | } ///:~
25 |
--------------------------------------------------------------------------------
/src/main/java/reusing/Jurassic.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // Making an entire class final.
3 |
4 | class SmallBrain {}
5 |
6 | final class Dinosaur {
7 | int i = 7;
8 | int j = 1;
9 | SmallBrain x = new SmallBrain();
10 | void f() {}
11 | }
12 |
13 | //! class Further extends Dinosaur {}
14 | // error: Cannot extend final class 'Dinosaur'
15 |
16 | public class Jurassic {
17 | public static void main(String[] args) {
18 | Dinosaur n = new Dinosaur();
19 | n.f();
20 | n.i = 40;
21 | n.j++;
22 | }
23 | } ///:~
24 |
--------------------------------------------------------------------------------
/src/main/java/reusing/Lisa.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // {CompileTimeError} (Won't compile)
3 |
4 | class Lisa extends Homer {
5 | @Override void doh(Milhouse m) {
6 | System.out.println("doh(Milhouse)");
7 | }
8 | } ///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/reusing/SpaceShip.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 |
3 | public class SpaceShip extends SpaceShipControls {
4 | private String name;
5 | public SpaceShip(String name) { this.name = name; }
6 | public String toString() { return name; }
7 | public static void main(String[] args) {
8 | SpaceShip protector = new SpaceShip("NSEA Protector");
9 | protector.forward(100);
10 | }
11 | } ///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/reusing/SpaceShipControls.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 |
3 | public class SpaceShipControls {
4 | void up(int velocity) {}
5 | void down(int velocity) {}
6 | void left(int velocity) {}
7 | void right(int velocity) {}
8 | void forward(int velocity) {}
9 | void back(int velocity) {}
10 | void turboBoost() {}
11 | } ///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/reusing/Wind.java:
--------------------------------------------------------------------------------
1 | package reusing;
2 | // Inheritance & upcasting.
3 |
4 | class Instrument {
5 | public void play() {}
6 | static void tune(Instrument i) {
7 | // ...
8 | i.play();
9 | }
10 | }
11 |
12 | // Wind objects are instruments
13 | // because they have the same interface:
14 | public class Wind extends Instrument {
15 | public static void main(String[] args) {
16 | Wind flute = new Wind();
17 | Instrument.tune(flute); // Upcasting
18 | }
19 | } ///:~
20 |
--------------------------------------------------------------------------------
/src/main/java/strings/ArrayListDisplay.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import generics.coffee.*;
3 | import java.util.*;
4 |
5 | public class ArrayListDisplay {
6 | public static void main(String[] args) {
7 | ArrayList coffees = new ArrayList();
8 | for(Coffee c : new CoffeeGenerator(10))
9 | coffees.add(c);
10 | System.out.println(coffees);
11 | }
12 | } /* Output:
13 | [Americano 0, Latte 1, Americano 2, Mocha 3, Mocha 4, Breve 5, Americano 6, Latte 7, Cappuccino 8, Cappuccino 9]
14 | *///:~
15 |
--------------------------------------------------------------------------------
/src/main/java/strings/Concatenation.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class Concatenation {
4 | public static void main(String[] args) {
5 | String mango = "mango";
6 | String s = "abc" + mango + "def" + 47;
7 | System.out.println(s);
8 | }
9 | } /* Output:
10 | abcmangodef47
11 | *///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/strings/DatabaseException.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class DatabaseException extends Exception {
4 | public DatabaseException(int transactionID, int queryID,
5 | String message) {
6 | super(String.format("(t%d, q%d) %s", transactionID,
7 | queryID, message));
8 | }
9 | public static void main(String[] args) {
10 | try {
11 | throw new DatabaseException(3, 7, "Write failed");
12 | } catch(Exception e) {
13 | System.out.println(e);
14 | }
15 | }
16 | } /* Output:
17 | DatabaseException: (t3, q7) Write failed
18 | *///:~
19 |
--------------------------------------------------------------------------------
/src/main/java/strings/Finding.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import java.util.regex.*;
3 | import static net.mindview.util.Print.*;
4 |
5 | public class Finding {
6 | public static void main(String[] args) {
7 | Matcher m = Pattern.compile("\\w+")
8 | .matcher("Evening is full of the linnet's wings");
9 | while(m.find())
10 | printnb(m.group() + " ");
11 | print();
12 | int i = 0;
13 | while(m.find(i)) {
14 | printnb(m.group() + " ");
15 | i++;
16 | }
17 | }
18 | } /* Output:
19 | Evening is full of the linnet s wings
20 | Evening vening ening ning ing ng g is is s full full ull ll l of of f the the he e linnet linnet innet nnet net et t s s wings wings ings ngs gs s
21 | *///:~
22 |
--------------------------------------------------------------------------------
/src/main/java/strings/Immutable.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import static net.mindview.util.Print.*;
3 |
4 | public class Immutable {
5 | public static String upcase(String s) {
6 | return s.toUpperCase();
7 | }
8 | public static void main(String[] args) {
9 | String q = "howdy";
10 | print(q); // howdy
11 | String qq = upcase(q);
12 | print(qq); // HOWDY
13 | print(q); // howdy
14 | }
15 | } /* Output:
16 | howdy
17 | HOWDY
18 | howdy
19 | *///:~
20 |
--------------------------------------------------------------------------------
/src/main/java/strings/InfiniteRecursion.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | // Accidental recursion.
3 | // {RunByHand}
4 | import java.util.*;
5 |
6 | public class InfiniteRecursion {
7 | public String toString() {
8 | return " InfiniteRecursion address: " + this + "\n";
9 | }
10 | public static void main(String[] args) {
11 | List v =
12 | new ArrayList();
13 | for(int i = 0; i < 10; i++)
14 | v.add(new InfiniteRecursion());
15 | System.out.println(v);
16 | }
17 | } ///:~
18 |
--------------------------------------------------------------------------------
/src/main/java/strings/IntegerMatch.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class IntegerMatch {
4 | public static void main(String[] args) {
5 | System.out.println("-1234".matches("-?\\d+"));
6 | System.out.println("5678".matches("-?\\d+"));
7 | System.out.println("+911".matches("-?\\d+"));
8 | System.out.println("+911".matches("(-|\\+)?\\d+"));
9 | }
10 | } /* Output:
11 | true
12 | true
13 | false
14 | true
15 | *///:~
16 |
--------------------------------------------------------------------------------
/src/main/java/strings/ReFlags.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import java.util.regex.*;
3 |
4 | public class ReFlags {
5 | public static void main(String[] args) {
6 | Pattern p = Pattern.compile("^java",
7 | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
8 | Matcher m = p.matcher(
9 | "java has regex\nJava has regex\n" +
10 | "JAVA has pretty good regular expressions\n" +
11 | "Regular expressions are in Java");
12 | while(m.find())
13 | System.out.println(m.group());
14 | }
15 | } /* Output:
16 | java
17 | Java
18 | JAVA
19 | *///:~
20 |
--------------------------------------------------------------------------------
/src/main/java/strings/Replacing.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import static net.mindview.util.Print.*;
3 |
4 | public class Replacing {
5 | static String s = Splitting.knights;
6 | public static void main(String[] args) {
7 | print(s.replaceFirst("f\\w+", "located"));
8 | print(s.replaceAll("shrubbery|tree|herring","banana"));
9 | }
10 | } /* Output:
11 | Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest... with... a herring!
12 | Then, when you have found the banana, you must cut down the mightiest banana in the forest... with... a banana!
13 | *///:~
14 |
--------------------------------------------------------------------------------
/src/main/java/strings/Resetting.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import java.util.regex.*;
3 |
4 | public class Resetting {
5 | public static void main(String[] args) throws Exception {
6 | Matcher m = Pattern.compile("[frb][aiu][gx]")
7 | .matcher("fix the rug with bags");
8 | while(m.find())
9 | System.out.print(m.group() + " ");
10 | System.out.println();
11 | m.reset("fix the rig with rags");
12 | while(m.find())
13 | System.out.print(m.group() + " ");
14 | }
15 | } /* Output:
16 | fix rug bag
17 | fix rig rag
18 | *///:~
19 |
--------------------------------------------------------------------------------
/src/main/java/strings/Rudolph.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class Rudolph {
4 | public static void main(String[] args) {
5 | for(String pattern : new String[]{ "Rudolph",
6 | "[rR]udolph", "[rR][aeiou][a-z]ol.*", "R.*" })
7 | System.out.println("Rudolph".matches(pattern));
8 | }
9 | } /* Output:
10 | true
11 | true
12 | true
13 | true
14 | *///:~
15 |
--------------------------------------------------------------------------------
/src/main/java/strings/ScannerDelimiter.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import java.util.*;
3 |
4 | public class ScannerDelimiter {
5 | public static void main(String[] args) {
6 | Scanner scanner = new Scanner("12, 42, 78, 99, 42");
7 | scanner.useDelimiter("\\s*,\\s*");
8 | while(scanner.hasNextInt())
9 | System.out.println(scanner.nextInt());
10 | }
11 | } /* Output:
12 | 12
13 | 42
14 | 78
15 | 99
16 | 42
17 | *///:~
18 |
--------------------------------------------------------------------------------
/src/main/java/strings/SimpleFormat.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class SimpleFormat {
4 | public static void main(String[] args) {
5 | int x = 5;
6 | double y = 5.332542;
7 | // The old way:
8 | System.out.println("Row 1: [" + x + " " + y + "]");
9 | // The new way:
10 | System.out.format("Row 1: [%d %f]\n", x, y);
11 | // or
12 | System.out.printf("Row 1: [%d %f]\n", x, y);
13 | }
14 | } /* Output:
15 | Row 1: [5 5.332542]
16 | Row 1: [5 5.332542]
17 | Row 1: [5 5.332542]
18 | *///:~
19 |
--------------------------------------------------------------------------------
/src/main/java/strings/SplitDemo.java:
--------------------------------------------------------------------------------
1 | package strings;
2 | import java.util.regex.*;
3 | import java.util.*;
4 | import static net.mindview.util.Print.*;
5 |
6 | public class SplitDemo {
7 | public static void main(String[] args) {
8 | String input =
9 | "This!!unusual use!!of exclamation!!points";
10 | print(Arrays.toString(
11 | Pattern.compile("!!").split(input)));
12 | // Only do the first three:
13 | print(Arrays.toString(
14 | Pattern.compile("!!").split(input, 3)));
15 | }
16 | } /* Output:
17 | [This, unusual use, of exclamation, points]
18 | [This, unusual use, of exclamation!!points]
19 | *///:~
20 |
--------------------------------------------------------------------------------
/src/main/java/strings/WhitherStringBuilder.java:
--------------------------------------------------------------------------------
1 | package strings;
2 |
3 | public class WhitherStringBuilder {
4 | public String implicit(String[] fields) {
5 | String result = "";
6 | for(int i = 0; i < fields.length; i++)
7 | result += fields[i];
8 | return result;
9 | }
10 | public String explicit(String[] fields) {
11 | StringBuilder result = new StringBuilder();
12 | for(int i = 0; i < fields.length; i++)
13 | result.append(fields[i]);
14 | return result.toString();
15 | }
16 | } ///:~
17 |
--------------------------------------------------------------------------------
/src/main/java/swt/DisplayEnvironment.java:
--------------------------------------------------------------------------------
1 | package swt;
2 | import swt.util.*;
3 | import org.eclipse.swt.*;
4 | import org.eclipse.swt.widgets.*;
5 | import org.eclipse.swt.layout.*;
6 | import java.util.*;
7 |
8 | public class DisplayEnvironment implements SWTApplication {
9 | public void createContents(Composite parent) {
10 | parent.setLayout(new FillLayout());
11 | Text text = new Text(parent, SWT.WRAP | SWT.V_SCROLL);
12 | for(Map.Entry entry: System.getenv().entrySet()) {
13 | text.append(entry.getKey() + ": " +
14 | entry.getValue() + "\n");
15 | }
16 | }
17 | public static void main(String [] args) {
18 | SWTConsole.run(new DisplayEnvironment(), 800, 600);
19 | }
20 | } ///:~
21 |
--------------------------------------------------------------------------------
/src/main/java/swt/HelloSWT.java:
--------------------------------------------------------------------------------
1 | package swt;
2 | // {Requires: org.eclipse.swt.widgets.Display; You must
3 | // install the SWT library from http://www.eclipse.org }
4 | import org.eclipse.swt.widgets.*;
5 |
6 | public class HelloSWT {
7 | public static void main(String [] args) {
8 | Display display = new Display();
9 | Shell shell = new Shell(display);
10 | shell.setText("Hi there, SWT!"); // Title bar
11 | shell.open();
12 | while(!shell.isDisposed())
13 | if(!display.readAndDispatch())
14 | display.sleep();
15 | display.dispose();
16 | }
17 | } ///:~
18 |
--------------------------------------------------------------------------------
/src/main/java/swt/util/SWTApplication.java:
--------------------------------------------------------------------------------
1 | //: swt/util/SWTApplication.java
2 | package swt.util;
3 | import org.eclipse.swt.widgets.*;
4 |
5 | public interface SWTApplication {
6 | void createContents(Composite parent);
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/swt/util/SWTConsole.java:
--------------------------------------------------------------------------------
1 | //: swt/util/SWTConsole.java
2 | package swt.util;
3 | import org.eclipse.swt.widgets.*;
4 |
5 | public class SWTConsole {
6 | public static void
7 | run(SWTApplication swtApp, int width, int height) {
8 | Display display = new Display();
9 | Shell shell = new Shell(display);
10 | shell.setText(swtApp.getClass().getSimpleName());
11 | swtApp.createContents(shell);
12 | shell.setSize(width, height);
13 | shell.open();
14 | while(!shell.isDisposed()) {
15 | if(!display.readAndDispatch())
16 | display.sleep();
17 | }
18 | display.dispose();
19 | }
20 | } ///:~
21 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/BoundedClassReferences.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 |
3 | public class BoundedClassReferences {
4 | public static void main(String[] args) {
5 | Class extends Number> bounded = int.class;
6 | bounded = double.class;
7 | bounded = Number.class;
8 | // Or anything else derived from Number.
9 | }
10 | } ///:~
11 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/ClassCasts.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 |
3 | class Building {}
4 | class House extends Building {}
5 |
6 | public class ClassCasts {
7 | public static void main(String[] args) {
8 | Building b = new House();
9 | Class houseType = House.class;
10 | House h = houseType.cast(b);
11 | h = (House)b; // ... or just do this.
12 | }
13 | } ///:~
14 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/GenericClassReferences.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 |
3 | public class GenericClassReferences {
4 | public static void main(String[] args) {
5 | Class intClass = int.class;
6 | Class genericIntClass = int.class;
7 | genericIntClass = Integer.class; // Same thing
8 | intClass = double.class;
9 | // genericIntClass = double.class; // Illegal
10 | }
11 | } ///:~
12 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/InterfaceViolation.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 | // Sneaking around an interface.
3 | import typeinfo.interfacea.*;
4 |
5 | class B implements A {
6 | public void f() {}
7 | public void g() {}
8 | }
9 |
10 | public class InterfaceViolation {
11 | public static void main(String[] args) {
12 | A a = new B();
13 | a.f();
14 | // a.g(); // Compile error
15 | System.out.println(a.getClass().getName());
16 | if(a instanceof B) {
17 | B b = (B)a;
18 | b.g();
19 | }
20 | }
21 | } /* Output:
22 | B
23 | *///:~
24 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/Operation.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 |
3 | public interface Operation {
4 | String description();
5 | void command();
6 | } ///:~
7 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/PetCount2.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 | import typeinfo.pets.*;
3 |
4 | public class PetCount2 {
5 | public static void main(String[] args) {
6 | PetCount.countPets(Pets.creator);
7 | }
8 | } /* (Execute to see output) *///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/Robot.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 | import java.util.*;
3 | import net.mindview.util.*;
4 |
5 | public interface Robot {
6 | String name();
7 | String model();
8 | List operations();
9 | class Test {
10 | public static void test(Robot r) {
11 | if(r instanceof Null)
12 | System.out.println("[Null Robot]");
13 | System.out.println("Robot name: " + r.name());
14 | System.out.println("Robot model: " + r.model());
15 | for(Operation operation : r.operations()) {
16 | System.out.println(operation.description());
17 | operation.command();
18 | }
19 | }
20 | }
21 | } ///:~
22 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/WildcardClassReferences.java:
--------------------------------------------------------------------------------
1 | package typeinfo;
2 |
3 | public class WildcardClassReferences {
4 | public static void main(String[] args) {
5 | Class> intClass = int.class;
6 | intClass = double.class;
7 | }
8 | } ///:~
9 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/factory/Factory.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/factory/Factory.java
2 | package typeinfo.factory;
3 | public interface Factory { T create(); } ///:~
4 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/interfacea/A.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/interfacea/A.java
2 | package typeinfo.interfacea;
3 |
4 | public interface A {
5 | void f();
6 | } ///:~
7 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/packageaccess/HiddenC.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/packageaccess/HiddenC.java
2 | package typeinfo.packageaccess;
3 | import typeinfo.interfacea.*;
4 | import static net.mindview.util.Print.*;
5 |
6 | class C implements A {
7 | public void f() { print("public C.f()"); }
8 | public void g() { print("public C.g()"); }
9 | void u() { print("package C.u()"); }
10 | protected void v() { print("protected C.v()"); }
11 | private void w() { print("private C.w()"); }
12 | }
13 |
14 | public class HiddenC {
15 | public static A makeA() { return new C(); }
16 | } ///:~
17 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Cat.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Cat.java
2 | package typeinfo.pets;
3 |
4 | public class Cat extends Pet {
5 | public Cat(String name) { super(name); }
6 | public Cat() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Cymric.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Cymric.java
2 | package typeinfo.pets;
3 |
4 | public class Cymric extends Manx {
5 | public Cymric(String name) { super(name); }
6 | public Cymric() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Dog.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Dog.java
2 | package typeinfo.pets;
3 |
4 | public class Dog extends Pet {
5 | public Dog(String name) { super(name); }
6 | public Dog() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/EgyptianMau.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/EgyptianMau.java
2 | package typeinfo.pets;
3 |
4 | public class EgyptianMau extends Cat {
5 | public EgyptianMau(String name) { super(name); }
6 | public EgyptianMau() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Hamster.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Hamster.java
2 | package typeinfo.pets;
3 |
4 | public class Hamster extends Rodent {
5 | public Hamster(String name) { super(name); }
6 | public Hamster() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Manx.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Manx.java
2 | package typeinfo.pets;
3 |
4 | public class Manx extends Cat {
5 | public Manx(String name) { super(name); }
6 | public Manx() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Mouse.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Mouse.java
2 | package typeinfo.pets;
3 |
4 | public class Mouse extends Rodent {
5 | public Mouse(String name) { super(name); }
6 | public Mouse() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Mutt.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Mutt.java
2 | package typeinfo.pets;
3 |
4 | public class Mutt extends Dog {
5 | public Mutt(String name) { super(name); }
6 | public Mutt() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Person.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Person.java
2 | package typeinfo.pets;
3 |
4 | public class Person extends Individual {
5 | public Person(String name) { super(name); }
6 | } ///:~
7 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Pet.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Pet.java
2 | package typeinfo.pets;
3 |
4 | public class Pet extends Individual {
5 | public Pet(String name) { super(name); }
6 | public Pet() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Pets.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Pets.java
2 | // Facade to produce a default PetCreator.
3 | package typeinfo.pets;
4 | import java.util.*;
5 |
6 | public class Pets {
7 | public static final PetCreator creator =
8 | new LiteralPetCreator();
9 | public static Pet randomPet() {
10 | return creator.randomPet();
11 | }
12 | public static Pet[] createArray(int size) {
13 | return creator.createArray(size);
14 | }
15 | public static ArrayList arrayList(int size) {
16 | return creator.arrayList(size);
17 | }
18 | } ///:~
19 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Pug.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Pug.java
2 | package typeinfo.pets;
3 |
4 | public class Pug extends Dog {
5 | public Pug(String name) { super(name); }
6 | public Pug() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Rat.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Rat.java
2 | package typeinfo.pets;
3 |
4 | public class Rat extends Rodent {
5 | public Rat(String name) { super(name); }
6 | public Rat() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/pets/Rodent.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/pets/Rodent.java
2 | package typeinfo.pets;
3 |
4 | public class Rodent extends Pet {
5 | public Rodent(String name) { super(name); }
6 | public Rodent() { super(); }
7 | } ///:~
8 |
--------------------------------------------------------------------------------
/src/main/java/typeinfo/toys/GenericToyTest.java:
--------------------------------------------------------------------------------
1 | //: typeinfo/toys/GenericToyTest.java
2 | // Testing class Class.
3 | package typeinfo.toys;
4 |
5 | public class GenericToyTest {
6 | public static void main(String[] args) throws Exception {
7 | Class ftClass = FancyToy.class;
8 | // Produces exact type:
9 | FancyToy fancyToy = ftClass.newInstance();
10 | Class super FancyToy> up = ftClass.getSuperclass();
11 | // This won't compile:
12 | // Class up2 = ftClass.getSuperclass();
13 | // Only produces Object:
14 | Object obj = up.newInstance();
15 | }
16 | } ///:~
17 |
--------------------------------------------------------------------------------