Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Technical Tips

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 
Tech Tips index

Tech Tips
December 15, 1998

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));
  }
} 

chiclets

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").

chiclets

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

coffeecup


Print Button
[ This page was updated: 21-Sep-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary | Feedback | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.