JavaLanguage Section Index | Page 5
How do I compute a Date 24 hours from now?
Date d = new Date(System.currentTimeMillis() + Timer.ONE_DAY);
How do I declare a method to accept a variable argument list?
Methods like printf allow you to pass multiple arguments to a method without having to explicitly declare versions for each "count" of arguments. For the final parameter type, use three periods, a...more
How do I enable assertions in a package and all its subpackages?
Add ... to the end of the top level package name, along with the required -ea / -enableassertions command line switch, as in java -ea:com.example.foo... FooClass.
How do I create an enumeration?
Starting in Java 5, you can use the new enum keyword, where you would specify a class or interface. For instance, for a enumeration of suits from a deck of cards, you might use: public enum Suit {...more
How do I create an enumeration?
Starting in Java 5, you can use the new enum keyword, where you would specify a class or interface. For instance, for a enumeration of suits from a deck of cards, you might use: public enum Suit {...more
How do I get the set of values from an enumeration?
If Suit is declared as an enum, named suit, then suit.values() will return an array of elements, one for each value in the enum set.more
What's a static import?
It lets you avoid qualifying static members with class names. If you import static java.lang.Math.PI; you can then use PI without qualifications.
What are the different types of annotations you can use in Java?
There are a number of annotations built in to the java 5.0 API.
Within java.lang.annotations, there are a number of annotations that can be used when working with annotations ("annotation to ann...more
How can I tell the difference between wait(timeout) timing out and actually being notified?
You can't directly by using wait and notify, but the can use a similar construct with the Lock interface of the java.util.concurrent.locks package, with its boolean tryLock(long time, TimeUnit uni...more
How do I use the for-each construct?
Any class that implements the Iterable interface can be used in a for-each statement: for (Datatype localvar : Iterable-instance). For instance, the following will print out each command-line argu...more
I have a bugfix and want to submit it to Sun. What do I do?
The best you can do is sign up as a contributor to Mustang, Sun's 6.0 release of Java SE. There is information on collaborating at https://mustang.dev.java.net/collaborate.html.more
I've see the letter P in hexidecimal constants, like 0XfP3. What does it mean?
As of JDK 5.0, the Java programming language now supports hexadecimal floating point literals. 0XfP3 = 120. The 0X prefix means what follows is hex. F = 15. P3 means 2 ^ 3 (2 raised to third power...more
Sometimes when I try to override a method, I get the signature wrong. The program still compiles, but the code never runs. How can I make sure my method actually overrides the method of the superclass?
The @Override annotation can be added to the javadoc for the new method. If you accidently miss an argument or capitalize the method name wrong, the compiler will generate a compile-time error.more
What does autoboxing and unboxing mean?
Autoboxing is not casting. Instead, it is the automatic conversion of a primitive data type to its wrapped Object type (boxing) and back (unboxing). For instance, Integer i = 3;, used to generate ...more
What purpose does the TimeUnit class have?
The TimeUnit class allows you to work with time quanities as both an amount and a unit of measurement. For instance, instead of always working with milliseconds when you want to put a thread to sl...more