/**
* Copyright (C) 2022 by Martin Robillard. See https://codesample.info/about.html
*/
package e2.chapter4;
import e2.chapter4.Deck.Shuffler;
public class Client
{
public static void main(String[] args)
{
// Illustration of the use of the Null object
CardSource source = new Deck();
System.out.println(source.());
source = CardSource.NULL;
System.out.println(source.());
// Illustration of the use of the Shuffler nested class
Deck deck = new Deck();
Shuffler = deck.newShuffler();
shuffler.shuffle();
shuffler.shuffle();
deck.shuffle();
System.out.println(shuffler.getNumberOfShuffles());
// Illustration of the use of the createByRankComparator static factory method
// The result should always be zero because both decks are freshly initialized.
System.out.println(Deck.createByRankComparator(Rank.ACE).compare(new Deck(), new Deck()));
}
}
This call to isNull
is dynamically bound to the default implementation
in the CardSource
interface.
This call to isNull
is dynamically bound to the default implementation
in the CardSource
interface.
This call to isNull
is dynamically bound to the overridden implementation
defined in the anonymous null object implementation.
This call to isNull
is dynamically bound to the overridden implementation
defined in the anonymous null object implementation.
The outer instance of shuffler
is deck
, because newShuffler()
was called
on this variable.
The outer instance of shuffler
is deck
, because newShuffler()
was called
on this variable.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2, 3); String d = cde.substring(1, 2);
The class String
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character
class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null
argument to a constructor or method in this class will cause a NullPointerException
to be thrown.
A String
represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character
class for more information). Index values refer to char
code units, so a supplementary character uses two positions in a String
.
The String
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char
values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator
class provides methods for finer-grain, locale-sensitive String comparison.
javac
compiler may implement the operator with StringBuffer
, StringBuilder
, or java.lang.invoke.StringConcatFactory
depending on the JDK version. The implementation of string conversion is typically through the method toString
, defined by Object
and inherited by all classes in Java.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
System
class contains several useful class fields and methods. It cannot be instantiated. Among the facilities provided by the System
class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
Console.charset()
if the Console
exists, stdout.encoding otherwise.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println
methods in class PrintStream
.
print(boolean)
and then println()
.
print(boolean)
and then println()
.
x
- The boolean
to be printed
print(int)
and then println()
.
print(int)
and then println()
.
x
- The int
to be printed.
The implementor must ensure that signum
(compare(x, y)) == -signum(compare(y, x))
for all x
and y
. (This implies that compare(x, y)
must throw an exception if and only if compare(y, x)
throws an exception.)
The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0))
implies compare(x, z)>0
.
Finally, the implementor must ensure that compare(x, y)==0
implies that signum(compare(x, z))==signum(compare(y, z))
for all z
.
(compare(x, y)==0) == (x.equals(y))
. Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."
o1
- the first object to be compared.
o2
- the second object to be compared.
NullPointerException
- if an argument is null and this comparator does not permit null arguments
ClassCastException
- if the arguments' types prevent them from being compared by this comparator.