Java 5 Information
From Kb
Contact Article Author | Blog of Article Author | FirstPartners.net Home | LinkedIn profile of Author
Contents |
For Each Loop
void doSomeLoop(Collection<TimerTask> c) {
for (TimerTask t : c){
t.doSomething();
}
}
More information on Sun Java Site: http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
Annotations
- More annotation information on Java 5 Tutorial Page
- Supress Warnings from compiler about unchecked casts and deprecated code
@SuppressWarnings({"unchecked", "deprecation"})
Static Imports
- Able to import a static method from another Class
import static MyClass.someStaticMethod; import static MyClass.*; //all methods
- Can call it using someStaticMethod() instead of having to go MyClass.someStaticMethod()
Assertions
- assert (someParam!=null);
Generics
Dynamically set return types - example from EasyMock
/** * Creates a mock object that implements the given interface, order checking * is enabled by default. * * @param <T> * the interface that the mock object should implement. * @param toMock * the class of the interface that the mock object should * implement. * @return the mock object. */ public static <T> T createStrictMock(Class<T> toMock) { return createStrictControl().createMock(toMock); }
Enums
Java Enum and the Switch Statement
- Can't use constants (even if they are int's) in a Switch statement, use Enum instead
- Samples taken from here. Sun's Enum overview is also quite useful. Sun Enum Javadoc gives more info.
- To declare the Enum
public enum Shape { RECTANGLE, CIRCLE, LINE }
- To use in the switch statement
switch (drawing) { case RECTANGLE: g.drawRect(x, y, width, height); break; case CIRCLE : g.drawOval(x, y, width, height); break; case LINE : g.drawLine(x, y, x + width, y + height); break; default: // could be used, but in general we cover all the members of the Enum above }
Looping over Enums
for (Shape shp : Shape.values()) { System.out.println(shp); // Prints RECTANGLE, CIRCLE, ... }
Refactoring (Integer) constants to Java Enum for use in the Switch Statement
- Problem: You want to do the bar minimum to use a switch statement with constants.
- Solution: Code something similar to the following.
- Note that this probably doesn't save time as a one-off, but can be useful if this code is used repeatedly, or if you need a half-way house while refactoring (part) of your code.
/** * Define an Enum (private) so that we can convert the int(eger) for use in a Switch statement * Normally Java needs a hard coded value in the switch statement , and will not accept a constant * (even if it is private static final int) * This can be defined in it's own file, or included at the bottom of a public class file */ enum Page{ PAGE_START(PageConstants.Page1), //where PageConstants give the (int) values we wish to convert PAGE_MIDDLE(PageConstants.Page), PAGE_END(PageConstants.Page2) private int pageInt; //internal holder /** * Enum constructor so that we can generate a suitable Enum value based on the int * @param page */ Page(int pageIndex){ this.pageInt=pageIndex; } /** * static convenience method to create Page Enum based on the int value passed in * we check if we can match the int value passed in to one of the constants in our collection. */ static Page valueOf(int pageValue){ for (Page p: Page.values()) { if(p.pageInt()== pageValue){ return p; } } throw new AssertionError("Unknown page value: " + pageValue); } }
- then to use (code snippet)
//Convert int to Enum Page thisPage = Page.valueOf(thisPageInt); // Now use the enum in the switch statement switch(thisPage){ case PAGE_START: /*do something */;break; case PAGE_END: /*do something */;break; default: //do nothing }
Java 5 JVM Arguments
Memory Size
- -Xmsn : Specify the initial size, in bytes, of the memory allocation pool. This value must be a multiple of 1024 greater than 1MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 2MB. Examples:
- -Xms384M
- Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB. Example
- -Xmx768m
- Size of the Permanent Generation. 5.0 and newer: 64 bit VMs are scaled 30% larger; 1.4 amd64: 96m; 1.3.1 -client: 32m.
- More info at http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
- -XX:MaxPermSize=512M
Threads
- Sample Sun Doc : http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html

