Bruce Eckel's Thinking in Java | Contents | Prev | Next |
//: ImmutableInteger.java // The Integer class cannot be changed import java.util.*; public class ImmutableInteger { public static void main(String[] args) { Vector v = new Vector(); for(int i = 0; i < 10; i++) v.addElement(new Integer(i)); // But how do you change the int // inside the Integer? } } ///:~
//: MutableInteger.java // A changeable wrapper class import java.util.*; class IntValue { int n; IntValue(int x) { n = x; } public String toString() { return Integer.toString(n); } } public class MutableInteger { public static void main(String[] args) { Vector v = new Vector(); for(int i = 0; i < 10; i++) v.addElement(new IntValue(i)); System.out.println(v); for(int i = 0; i < v.size(); i++) ((IntValue)v.elementAt(i)).n++; System.out.println(v); } } ///:~
//: Immutable1.java // Objects that cannot be modified // are immune to aliasing. public class Immutable1 { private int data; public Immutable1(int initVal) { data = initVal; } public int read() { return data; } public boolean nonzero() { return data != 0; } public Immutable1 quadruple() { return new Immutable1(data * 4); } static void f(Immutable1 i1) { Immutable1 quad = i1.quadruple(); System.out.println("i1 = " + i1.read()); System.out.println("quad = " + quad.read()); } public static void main(String[] args) { Immutable1 x = new Immutable1(47); System.out.println("x = " + x.read()); f(x); System.out.println("x = " + x.read()); } } ///:~
//: Immutable2.java // A companion class for making changes // to immutable objects. class Mutable { private int data; public Mutable(int initVal) { data = initVal; } public Mutable add(int x) { data += x; return this; } public Mutable multiply(int x) { data *= x; return this; } public Immutable2 makeImmutable2() { return new Immutable2(data); } } public class Immutable2 { private int data; public Immutable2(int initVal) { data = initVal; } public int read() { return data; } public boolean nonzero() { return data != 0; } public Immutable2 add(int x) { return new Immutable2(data + x); } public Immutable2 multiply(int x) { return new Immutable2(data * x); } public Mutable makeMutable() { return new Mutable(data); } public static Immutable2 modify1(Immutable2 y){ Immutable2 val = y.add(12); val = val.multiply(3); val = val.add(11); val = val.multiply(2); return val; } // This produces the same result: public static Immutable2 modify2(Immutable2 y){ Mutable m = y.makeMutable(); m.add(12).multiply(3).add(11).multiply(2); return m.makeImmutable2(); } public static void main(String[] args) { Immutable2 i2 = new Immutable2(47); Immutable2 r1 = modify1(i2); Immutable2 r2 = modify2(i2); System.out.println("i2 = " + i2.read()); System.out.println("r1 = " + r1.read()); System.out.println("r2 = " + r2.read()); } } ///:~
//: Stringer.java public class Stringer { static String upcase(String s) { return s.toUpperCase(); } public static void main(String[] args) { String q = new String("howdy"); System.out.println(q); // howdy String qq = upcase(q); System.out.println(qq); // HOWDY System.out.println(q); // howdy } } ///:~
String x = Stringer.upcase(s);
//: ImmutableStrings.java // Demonstrating StringBuffer public class ImmutableStrings { public static void main(String[] args) { String foo = "foo"; String s = "abc" + foo + "def" + Integer.toString(47); System.out.println(s); // The "equivalent" using StringBuffer: StringBuffer sb = new StringBuffer("abc"); // Creates String! sb.append(foo); sb.append("def"); // Creates String! sb.append(Integer.toString(47)); System.out.println(sb); } } ///:~
Method
|
Arguments,
overloading
|
Use
|
---|---|---|
Constructor
|
Overloaded:
default, length of buffer to create,
String
to create from.
|
Create
a new
StringBuffer
object.
|
toString( )
|
Creates
a
String
from this
StringBuffer.
|
|
length( )
|
Number
of characters in the
StringBuffer.
|
|
capacity( )
|
Returns
current number of spaces allocated.
|
|
ensure-
Capacity( ) |
Integer
indicating desired capacity.
|
Makes
the
StringBuffer
hold at least the desired number of spaces.
|
setLength( )
|
Integer
indicating new length of character string in buffer.
|
Truncates
or expands the previous character string. If expanding, pads with nulls.
|
charAt( )
|
Integer
indicating the location of the desired element.
|
Returns
the
char
at that location in the buffer.
|
setCharAt( )
|
Integer
indicating the location of the desired element and the new
char
value for the element.
|
Modifies
the value at that location.
|
getChars( )
|
The
beginning and end from which to copy, the array to copy into, an index into the
destination array.
|
Copy
chars
into an external array. There’s no
getBytes( )
as in
String.
|
append( )
|
Overloaded:
Object,
String,
char[],
char[]
with offset and length,
boolean,
char,
int,
long,
float,
double.
|
The
argument is converted to a string and appended to the end of the current
buffer, increasing the buffer if necessary.
|
insert( )
|
Overloaded,
each with a first argument of the offset at which to start inserting:
Object,
String,
char[],
boolean,
char,
int,
long,
float,
double.
|
The
second argument is converted to a string and inserted into the current buffer
beginning at the offset. The buffer is increased if necessary.
|
reverse( )
|
The
order of the characters in the buffer is reversed.
|