This issue presents tips, techniques, and sample code for the
following topics:
True/False Random Values with NextBoolean
nextBoolean is a new method, which is part of the
java.util.Random class. It returns a random false/true
value, and is useful if you have an application that effectively needs
to flip a coin.
Random numbers are especially useful when generating test data.
This program prints out 10 values of "heads" or
"tails":
import java.util.Random;
public class flip {
public static void main(String args[])
{
Random rn = new Random();
for (int i = 1; i <= 10; i++)
System.out.println(rn.nextBoolean() ?
"heads" : "tails");
}
}
If you've studied random numbers at all, especially the method used by Java
(the linear congruential method), you might recall that one problem with
generated random numbers is that the low-order bits tend to be less random
than the high-order ones. This problem is worked around by internally
generating a sequence of 48-bit random values, and then using at most 32 of
the high-order bits of any given value. nextBoolean uses a single high
bit, while nextInt uses 32 bits of the 48, ignoring the lowest 16 bits.
See Knuth's The Art of Computer Programming section 3.2.1 for a
discussion of random number generators.
Another new feature in Random is the ability to generate
random integers in a specified range, using nextInt .
nextInt(N) returns a random integer in the range:
0 <= number < N
So this program will generate a sequence of 10 numbers, each in the
range 0-9 inclusive:
import java.util.Random;
public class rand {
public static void main(String args[])
{
Random rn = new Random();
for (int i = 1; i <= 10; i++)
System.out.println(rn.nextInt(10));
}
}
Cross Compilation Using -Target
A new javac option "-target" has been added to allow specification
of a particular JavaTM Virtual Machine
(JVM) target version. By default, the Java 2 version of javac produces
.class files compatible with both 1.1 and 1.2 versions of
the JVM. But you can change this. For example, a simple program such as:
public class hello {
public static void main (String args[])
{
System.out.println("hello world");
}
}
when compiled with:
$ javac hello.java
will run correctly with both JDK 1.1 and Java 2 versions, but
when compiled with:
$ javac -target 1.2 hello.java
will work only with the Java 2 version, failing with a Can't find
class hello error message otherwise. This feature can be used to force
compatibility with both JDK 1.1 and Java 2 (by saying "-target 1.1"),
or to force Java 2 (by saying "-target 1.2").
Unicode Character Blocks
Java uses the Unicode character set, which contains a series of subsections
known as character blocks. For example, the letter "z" is part
of the BASIC_LATIN block, while the character "\u0400" is within
the CYRILLIC block.
This program shows how such blocks can be used:
public class uniblock {
public static void main(String args[])
{
char c;
c = 'z';
Character.UnicodeBlock block1 =
Character.UnicodeBlock.of(c);
c = '\u0400';
Character.UnicodeBlock block2 =
Character.UnicodeBlock.of(c);
System.out.println("block 1 = " + block1);
System.out.println("block 2 = " + block2);
System.out.println("Are blocks equal? " +
(block1 == block2));
}
}
The output is:
block 1 = BASIC_LATIN
block 2 = CYRILLIC
Are blocks equal? false
This feature is useful in classifying characters. For example, the
BASIC_LATIN category corresponds to the characters in the range \u0000
through \u007F, the values in the widely used, seven-bit ASCII character
set
|