JDC Tech Tips Vol. 2 No. 5

JDCTechTips@sun.com
Wed, 16 Dec 1998 05:49:30 GMT

-WELCOME- to the Java Developer Connection(sm) Tech Tips, Vol.2 No.5.
This issue presents three tips, each of them describing new features in
Java(tm) 2 (formerly code-named "JDK(tm) 1.2").

J D C T E C H T I P S

TIPS, TECHNIQUES, AND SAMPLE CODE
* True/False Random Values with NextBoolean
* Cross Compilation Using -Target
* Unicode Character Blocks

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T I P S , T E C H N I Q U E S , A N D S A M P L E C O D E

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 Java 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

. . . . . . . . . . . . . . . . . . . . . . . .
-- NOTE --
The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list,
see Subscribe/Unsubscribe below.

-- FEEDBACK --
Comments? Send your feedback on the JDC Tech Tips to:

JDCTechTips@Sun.com

-- SUBSCRIBE/UNSUBSCRIBE --
The JDC Tech Tips are sent to you because you elected to subscribe
when you registered as a JDC member. To unsubscribe from JDC Email,
go to the following address and enter the email address you wish to
remove from the mailing list:

http://developer.java.sun.com/unsubscribe.html

To become a JDC member and subscribe to this newsletter go to:

http://java.sun.com/jdc/


-- ARCHIVES --
You'll find the JDC Tech Tips archives at:

http://developer.java.sun.com/developer/javaInDepth/TechTips/index.html

-- COPYRIGHT --
Copyright 1998 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://developer.java.sun.com/developer/copyright.html

The JDC Tech Tips are written by Glen McCluskey.

JDC Tech Tips Vol. 2 No. 5
December 15, 1998