Book edits

This commit is contained in:
Bruce Eckel 2015-12-18 11:28:19 -08:00
parent 634d5df852
commit 8219ea08b2
41 changed files with 202 additions and 212 deletions

View File

@ -8,9 +8,9 @@ public class ArrayOfGenericType<T> {
T[] array; // OK
@SuppressWarnings("unchecked")
public ArrayOfGenericType(int size) {
//! array = new T[size]; // Illegal
//- array = new T[size]; // Illegal
array = (T[])new Object[size]; // "unchecked" Warning
}
// Illegal:
//! public <U> U[] makeArray() { return new U[10]; }
//- public <U> U[] makeArray() { return new U[10]; }
}

View File

@ -13,7 +13,7 @@ public class ArrayOfGenerics {
ls = (List<String>[])la; // "Unchecked" warning
ls[0] = new ArrayList<>();
// Compile-time checking produces an error:
//! ls[1] = new ArrayList<Integer>();
//- ls[1] = new ArrayList<Integer>();
// The problem: List<String> is a subtype of Object
Object[] objects = ls; // So assignment is OK

View File

@ -49,8 +49,8 @@ public class ArrayOptions {
g[i] = i*i;
int[] h = { 11, 47, 93 };
// Compile error: variable e not initialized:
//!print("e.length = " + e.length);
// Compile error: variable e not initialized:
//- print("e.length = " + e.length);
System.out.println("f.length = " + f.length);
System.out.println("g.length = " + g.length);
System.out.println("h.length = " + h.length);

View File

@ -13,7 +13,7 @@ public class ArrayIsNotIterable {
test(Arrays.asList(1, 2, 3));
String[] strings = { "A", "B", "C" };
// An array works in for-in, but it's not Iterable:
//! test(strings);
//- test(strings);
// You must explicitly convert it to an Iterable:
test(Arrays.asList(strings));
}

View File

@ -14,18 +14,18 @@ public class ReadOnly {
Collections.unmodifiableCollection(
new ArrayList<>(data));
System.out.println(c); // Reading is OK
//! c.add("one"); // Can't change it
//- c.add("one"); // Can't change it
List<String> a = Collections.unmodifiableList(
new ArrayList<>(data));
ListIterator<String> lit = a.listIterator();
System.out.println(lit.next()); // Reading is OK
//! lit.add("one"); // Can't change it
//- lit.add("one"); // Can't change it
Set<String> s = Collections.unmodifiableSet(
new HashSet<>(data));
System.out.println(s); // Reading is OK
//! s.add("one"); // Can't change it
//- s.add("one"); // Can't change it
// For a SortedSet:
Set<String> ss = Collections.unmodifiableSortedSet(
@ -34,7 +34,7 @@ public class ReadOnly {
Map<String,String> m = Collections.unmodifiableMap(
new HashMap<>(Countries.capitals(6)));
System.out.println(m); // Reading is OK
//! m.put("Ralph", "Howdy!");
//- m.put("Ralph", "Howdy!");
// For a SortedMap:
Map<String,String> sm =

View File

@ -27,7 +27,7 @@ public class ResponsiveUI extends Thread {
}
}
public static void main(String[] args) throws Exception {
//! new UnresponsiveUI(); // Must kill this process
//- new UnresponsiveUI(); // Must kill this process
new ResponsiveUI();
System.in.read();
System.out.println(d); // Shows progress

View File

@ -7,14 +7,14 @@ import static onjava.Range.*;
public class BreakAndContinue {
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
for(int i = 0; i < 100; i++) { // (1)
if(i == 74) break; // Out of for loop
if(i % 9 != 0) continue; // Next iteration
System.out.print(i + " ");
}
System.out.println();
// Using for-in:
for(int i : range(100)) {
for(int i : range(100)) { // (2)
if(i == 74) break; // Out of for loop
if(i % 9 != 0) continue; // Next iteration
System.out.print(i + " ");
@ -22,7 +22,7 @@ public class BreakAndContinue {
System.out.println();
int i = 0;
// An "infinite loop":
while(true) {
while(true) { // (3)
i++;
int j = i * 27;
if(j == 1269) break; // Out of loop

View File

@ -8,7 +8,7 @@ public class IfElse {
static void test(int testval, int target) {
if(testval > target)
result = +1;
else if(testval < target)
else if(testval < target) // (1)
result = -1;
else
result = 0; // Match

View File

@ -1,16 +1,15 @@
// control/IfElse2.java
// control/TestWithReturn.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
public class IfElse2 {
public class TestWithReturn {
static int test(int testval, int target) {
if(testval > target)
return +1;
else if(testval < target)
if(testval < target)
return -1;
else
return 0; // Match
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10, 5));

15
control/TrueFalse.java Normal file
View File

@ -0,0 +1,15 @@
// control/TrueFalse.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
public class TrueFalse {
public static void main(String[] args) {
System.out.println(1 == 1);
System.out.println(1 == 2);
}
}
/* Output:
true
false
*/

View File

@ -13,11 +13,12 @@
<jrun cls="ForInInt" />
<jrun cls="ForInString" />
<jrun cls="IfElse" />
<jrun cls="IfElse2" />
<jrun cls="LabeledFor" />
<jrun cls="LabeledWhile" />
<jrun cls="ListCharacters" />
<jrun cls="StringSwitch" />
<jrun cls="TestWithReturn" />
<jrun cls="TrueFalse" />
<jrun cls="VowelsAndConsonants" />
<jrun cls="WhileTest" />
</target>
@ -29,11 +30,12 @@
<jrunconsole cls="ForInInt" />
<jrunconsole cls="ForInString" />
<jrunconsole cls="IfElse" />
<jrunconsole cls="IfElse2" />
<jrunconsole cls="LabeledFor" />
<jrunconsole cls="LabeledWhile" />
<jrunconsole cls="ListCharacters" />
<jrunconsole cls="StringSwitch" />
<jrunconsole cls="TestWithReturn" />
<jrunconsole cls="TrueFalse" />
<jrunconsole cls="VowelsAndConsonants" />
<jrunconsole cls="WhileTest" />
</target>

View File

@ -37,10 +37,10 @@ class StormyInning extends Inning implements Storm {
public StormyInning(String s)
throws Foul, BaseballException {}
// Regular methods must conform to base class:
//! void walk() throws PopFoul {} //Compile error
//- void walk() throws PopFoul {} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class:
//! public void event() throws RainedOut {}
//- public void event() throws RainedOut {}
// If the method doesn't already exist in the
// base class, the exception is OK:
@Override

View File

@ -9,14 +9,14 @@ public class ArrayOfGeneric {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// Compiles; produces ClassCastException:
//! gia = (Generic<Integer>[])new Object[SIZE];
//- gia = (Generic<Integer>[])new Object[SIZE];
// Runtime type is the raw (erased) type:
gia = (Generic<Integer>[])new Generic[SIZE];
System.out.println(gia.getClass().getSimpleName());
gia[0] = new Generic<>();
//! gia[1] = new Object(); // Compile-time error
//- gia[1] = new Object(); // Compile-time error
// Discovers type mismatch at compile time:
//! gia[2] = new Generic<Double>();
//- gia[2] = new Generic<Double>();
}
}
/* Output:

View File

@ -18,7 +18,7 @@ public class GenericArray<T> {
public static void main(String[] args) {
GenericArray<Integer> gai = new GenericArray<>(10);
// This causes a ClassCastException:
//! Integer[] ia = gai.rep();
//- Integer[] ia = gai.rep();
// This is OK:
Object[] oa = gai.rep();
}

View File

@ -41,10 +41,10 @@ public class Decoration {
TimeStamped t = new TimeStamped(new Basic());
TimeStamped t2 = new TimeStamped(
new SerialNumbered(new Basic()));
//! t2.getSerialNumber(); // Not available
//- t2.getSerialNumber(); // Not available
SerialNumbered s = new SerialNumbered(new Basic());
SerialNumbered s2 = new SerialNumbered(
new TimeStamped(new Basic()));
//! s2.getStamp(); // Not available
//- s2.getStamp(); // Not available
}
}

View File

@ -10,7 +10,7 @@ public class ChocolateChip extends Cookie {
System.out.println("ChocolateChip constructor");
}
public void chomp() {
//! bite(); // Can't access bite
//- bite(); // Can't access bite
}
public static void main(String[] args) {
ChocolateChip x = new ChocolateChip();

View File

@ -8,7 +8,7 @@ import hiding.dessert.*;
public class Dinner {
public static void main(String[] args) {
Cookie x = new Cookie();
//! x.bite(); // Can't access
//- x.bite(); // Can't access
}
}
/* Output:

View File

@ -13,7 +13,7 @@ class Sundae {
public class IceCream {
public static void main(String[] args) {
//! Sundae x = new Sundae();
//- Sundae x = new Sundae();
Sundae x = Sundae.makeASundae();
}
}

View File

@ -25,7 +25,7 @@ class Soup2 {
public class Lunch {
void testPrivate() {
// Can't do this! Private constructor:
//! Soup1 soup = new Soup1();
//- Soup1 soup = new Soup1();
}
void testStatic() {
Soup1 soup = Soup1.makeSoup();

View File

@ -2,49 +2,27 @@
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Demotion of primitives and overloading.
// Demotion of primitives.
public class Demotion {
void f1(char x) { System.out.println("f1(char)"); }
void f1(byte x) { System.out.println("f1(byte)"); }
void f1(short x) { System.out.println("f1(short)"); }
void f1(int x) { System.out.println("f1(int)"); }
void f1(long x) { System.out.println("f1(long)"); }
void f1(float x) { System.out.println("f1(float)"); }
void f1(double x) { System.out.println("f1(double)"); }
void f2(char x) { System.out.println("f2(char)"); }
void f2(byte x) { System.out.println("f2(byte)"); }
void f2(short x) { System.out.println("f2(short)"); }
void f2(int x) { System.out.println("f2(int)"); }
void f2(long x) { System.out.println("f2(long)"); }
void f2(float x) { System.out.println("f2(float)"); }
void f3(char x) { System.out.println("f3(char)"); }
void f3(byte x) { System.out.println("f3(byte)"); }
void f3(short x) { System.out.println("f3(short)"); }
void f3(int x) { System.out.println("f3(int)"); }
void f3(long x) { System.out.println("f3(long)"); }
void f4(char x) { System.out.println("f4(char)"); }
void f4(byte x) { System.out.println("f4(byte)"); }
void f4(short x) { System.out.println("f4(short)"); }
void f4(int x) { System.out.println("f4(int)"); }
void f5(char x) { System.out.println("f5(char)"); }
void f5(byte x) { System.out.println("f5(byte)"); }
void f5(short x) { System.out.println("f5(short)"); }
void f6(char x) { System.out.println("f6(char)"); }
void f6(byte x) { System.out.println("f6(byte)"); }
void f7(char x) { System.out.println("f7(char)"); }
void testDouble() {
double x = 0;
System.out.println("double argument:");
f1(x);f2((float)x);f3((long)x);f4((int)x);
f5((short)x);f6((byte)x);f7((char)x);
f1(x);
f2((float)x);
f3((long)x);
f4((int)x);
f5((short)x);
f6((byte)x);
f7((char)x);
}
public static void main(String[] args) {
Demotion p = new Demotion();

View File

@ -20,7 +20,7 @@ public class Flower {
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
//- this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
@ -29,7 +29,7 @@ public class Flower {
System.out.println("no-arg constructor");
}
void printPetalCount() {
//! this(11); // Not inside non-constructor!
//- this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}

View File

@ -15,34 +15,30 @@ public class InitialValues {
double d;
InitialValues reference;
void printInitialValues() {
System.out.println("Data type Initial value");
System.out.println("boolean " + t);
System.out.println("char [" + c + "]");
System.out.println("byte " + b);
System.out.println("short " + s);
System.out.println("int " + i);
System.out.println("long " + l);
System.out.println("float " + f);
System.out.println("double " + d);
System.out.println("reference " + reference);
System.out.println("Data type Initial value");
System.out.println("boolean " + t);
System.out.println("char [" + c + "]");
System.out.println("byte " + b);
System.out.println("short " + s);
System.out.println("int " + i);
System.out.println("long " + l);
System.out.println("float " + f);
System.out.println("double " + d);
System.out.println("reference " + reference);
}
public static void main(String[] args) {
InitialValues iv = new InitialValues();
iv.printInitialValues();
/* You can also say:
new InitialValues().printInitialValues();
*/
}
}
/* Output:
Data type Initial value
boolean false
char [NUL]
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
reference null
Data type Initial value
boolean false
char [NUL]
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
reference null
*/

View File

@ -3,7 +3,7 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
public class MethodInit3 {
//! int j = g(i); // Illegal forward reference
//- int j = g(i); // Illegal forward reference
int i = f();
int f() { return 11; }
int g(int n) { return n * 10; }

View File

@ -2,7 +2,7 @@
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// Java "Instance Initialization."
// Instance initialization.
class Mug {
Mug(int marker) {
@ -16,7 +16,7 @@ class Mug {
public class Mugs {
Mug mug1;
Mug mug2;
{
{ // (1)
mug1 = new Mug(1);
mug2 = new Mug(2);
System.out.println("mug1 & mug2 initialized");

View File

@ -10,7 +10,7 @@ class Bird2 {
public class NoSynthesis {
public static void main(String[] args) {
//! Bird2 b = new Bird2(); // No default
//- Bird2 b = new Bird2(); // No default
Bird2 b2 = new Bird2(1);
Bird2 b3 = new Bird2(1.0);
}

View File

@ -25,7 +25,7 @@ public class OverloadingVarargs {
f(2, 1);
f(0);
f(0L);
//! f(); // Won't compile -- ambiguous
//- f(); // Won't compile -- ambiguous
}
}
/* Output:

View File

@ -9,7 +9,7 @@ class WithInner {
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
//- InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}

View File

@ -18,7 +18,7 @@ public class Parcel6 {
String s = ts.getSlip();
}
// Can't use it here! Out of scope:
//! TrackingSlip ts = new TrackingSlip("x");
//- TrackingSlip ts = new TrackingSlip("x");
}
public void track() { internalTracking(true); }
public static void main(String[] args) {

View File

@ -31,6 +31,6 @@ public class TestParcel {
Contents c = p.contents();
Destination d = p.destination("Tasmania");
// Illegal -- can't access private class:
//! Parcel4.PContents pc = p.new PContents();
//- Parcel4.PContents pc = p.new PContents();
}
}

View File

@ -27,5 +27,5 @@ class C4 extends C implements I3 {
}
// Methods differ only by return type:
//! class C5 extends C implements I1 {}
//! interface I4 extends I1, I3 {}
//- class C5 extends C implements I1 {}
//- interface I4 extends I1, I3 {}

View File

@ -56,7 +56,7 @@ interface E {
}
void g();
// Cannot be private within an interface:
//! private interface I {}
//- private interface I {}
}
public class NestingInterfaces {
@ -70,9 +70,9 @@ public class NestingInterfaces {
}
// Cannot implement a private interface except
// within that interface's defining class:
//! class DImp implements A.D {
//! public void f() {}
//! }
//- class DImp implements A.D {
//- public void f() {}
//- }
class EImp implements E {
@Override
public void g() {}
@ -92,11 +92,11 @@ public class NestingInterfaces {
public static void main(String[] args) {
A a = new A();
// Can't access A.D:
//! A.D ad = a.getD();
//- A.D ad = a.getD();
// Doesn't return anything but A.D:
//! A.DImp2 di2 = a.getD();
//- A.DImp2 di2 = a.getD();
// Cannot access a member of the interface:
//! a.getD().f();
//- a.getD().f();
// Only another A can do anything with getD():
A a2 = new A();
a2.receiveD(a.getD());

View File

@ -10,53 +10,53 @@ public class AllOps {
void f(boolean b) {}
void boolTest(boolean x, boolean y) {
// Arithmetic operators:
//! x = x * y;
//! x = x / y;
//! x = x % y;
//! x = x + y;
//! x = x - y;
//! x++;
//! x--;
//! x = +y;
//! x = -y;
//- x = x * y;
//- x = x / y;
//- x = x % y;
//- x = x + y;
//- x = x - y;
//- x++;
//- x--;
//- x = +y;
//- x = -y;
// Relational and logical:
//! f(x > y);
//! f(x >= y);
//! f(x < y);
//! f(x <= y);
//- f(x > y);
//- f(x >= y);
//- f(x < y);
//- f(x <= y);
f(x == y);
f(x != y);
f(!y);
x = x && y;
x = x || y;
// Bitwise operators:
//! x = ~y;
//- x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
// Compound assignment:
//! x += y;
//! x -= y;
//! x *= y;
//! x /= y;
//! x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//- x += y;
//- x -= y;
//- x *= y;
//- x /= y;
//- x %= y;
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting:
//! char c = (char)x;
//! byte b = (byte)x;
//! short s = (short)x;
//! int i = (int)x;
//! long l = (long)x;
//! float f = (float)x;
//! double d = (double)x;
//- char c = (char)x;
//- byte b = (byte)x;
//- short s = (short)x;
//- int i = (int)x;
//- long l = (long)x;
//- float f = (float)x;
//- double d = (double)x;
}
void charTest(char x, char y) {
// Arithmetic operators:
@ -76,9 +76,9 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
x= (char)~y;
x = (char)(x & y);
@ -100,7 +100,7 @@ public class AllOps {
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
byte b = (byte)x;
short s = (short)x;
int i = (int)x;
@ -126,9 +126,9 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
x = (byte)~y;
x = (byte)(x & y);
@ -150,7 +150,7 @@ public class AllOps {
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
short s = (short)x;
int i = (int)x;
@ -176,9 +176,9 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
x = (short)~y;
x = (short)(x & y);
@ -200,7 +200,7 @@ public class AllOps {
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
int i = (int)x;
@ -226,9 +226,9 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
x = ~y;
x = x & y;
@ -250,7 +250,7 @@ public class AllOps {
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
@ -276,9 +276,9 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
x = ~y;
x = x & y;
@ -300,7 +300,7 @@ public class AllOps {
x ^= y;
x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
@ -326,31 +326,31 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
//- x = ~y;
//- x = x & y;
//- x = x | y;
//- x = x ^ y;
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
//- x &= y;
//- x ^= y;
//- x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;
@ -376,31 +376,31 @@ public class AllOps {
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
//- f(!x);
//- f(x && y);
//- f(x || y);
// Bitwise operators:
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
//- x = ~y;
//- x = x & y;
//- x = x | y;
//- x = x ^ y;
//- x = x << 1;
//- x = x >> 1;
//- x = x >>> 1;
// Compound assignment:
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
//- x <<= 1;
//- x >>= 1;
//- x >>>= 1;
//- x &= y;
//- x ^= y;
//- x |= y;
// Casting:
//! boolean bl = (boolean)x;
//- boolean bl = (boolean)x;
char c = (char)x;
byte b = (byte)x;
short s = (short)x;

View File

@ -19,9 +19,9 @@ public class Bool {
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
// Treating an int as a boolean is not legal Java:
//! System.out.println("i && j is " + (i && j));
//! System.out.println("i || j is " + (i || j));
//! System.out.println("!i is " + !i);
//- System.out.println("i && j is " + (i && j));
//- System.out.println("i || j is " + (i || j));
//- System.out.println("!i is " + !i);
System.out.println("(i < 10) && (j < 10) is "
+ ((i < 10) && (j < 10)) );
System.out.println("(i < 10) || (j < 10) is "

View File

@ -29,7 +29,7 @@ public class RTTI {
x[0].f();
x[1].g();
// Compile time: method not found in Useful:
//! x[1].u();
//- x[1].u();
((MoreUseful)x[1]).u(); // Downcast/RTTI
((MoreUseful)x[0]).u(); // Exception thrown
}

View File

@ -90,7 +90,7 @@ public class CheckCloneable {
Ordinary x = new Ordinary();
// This won't compile;
// clone() is protected in Object:
//! x = (Ordinary)x.clone();
//- x = (Ordinary)x.clone();
// Checks first to see if a class
// implements Cloneable:
for(Ordinary ord1 : ord) {

View File

@ -29,8 +29,8 @@ public class HorrorFlick {
Hero h = new Hero();
Scientist s = new Scientist();
MadScientist m = new MadScientist();
//! p = (Person)p.clone(); // Compile error
//! h = (Hero)h.clone(); // Compile error
//- p = (Person)p.clone(); // Compile error
//- h = (Hero)h.clone(); // Compile error
s = (Scientist)s.clone();
m = (MadScientist)m.clone();
}

View File

@ -10,7 +10,7 @@ class Gizmo {
public class FinalArguments {
void with(final Gizmo g) {
//! g = new Gizmo(); // Illegal -- g is final
//- g = new Gizmo(); // Illegal -- g is final
}
void without(Gizmo g) {
g = new Gizmo(); // OK -- g not final

View File

@ -33,14 +33,14 @@ public class FinalData {
}
public static void main(String[] args) {
FinalData fd1 = new FinalData("fd1");
//! fd1.valueOne++; // Error: can't change value
//- fd1.valueOne++; // Error: can't change value
fd1.v2.i++; // Object isn't constant!
fd1.v1 = new Value(9); // OK -- not final
for(int i = 0; i < fd1.a.length; i++)
fd1.a[i]++; // Object isn't constant!
//! fd1.v2 = new Value(0); // Error: Can't
//! fd1.VAL_3 = new Value(1); // change reference
//! fd1.a = new int[3];
//- fd1.v2 = new Value(0); // Error: Can't
//- fd1.VAL_3 = new Value(1); // change reference
//- fd1.a = new int[3];
System.out.println(fd1);
System.out.println("Creating new FinalData");
FinalData fd2 = new FinalData("fd2");

View File

@ -42,12 +42,12 @@ public class FinalOverridingIllusion {
// You can upcast:
OverridingPrivate op = op2;
// But you can't call the methods:
//! op.f();
//! op.g();
//- op.f();
//- op.g();
// Same here:
WithFinals wf = op2;
//! wf.f();
//! wf.g();
//- wf.f();
//- wf.g();
}
}
/* Output:

View File

@ -13,7 +13,7 @@ final class Dinosaur {
void f() {}
}
//! class Further extends Dinosaur {}
//- class Further extends Dinosaur {}
// error: Cannot extend final class 'Dinosaur'
public class Jurassic {

View File

@ -56,8 +56,8 @@ public class Blips {
b1 = (Blip1)in.readObject();
}
// OOPS! Throws an exception:
//! System.out.println("Recovering b2:");
//! b2 = (Blip2)in.readObject();
//- System.out.println("Recovering b2:");
//- b2 = (Blip2)in.readObject();
}
}
/* Output: