J2SE 6.0 (Mustang) Sample Code
JDK 6.0 release candidates are now availabile on Zamples.com.
Javadoc is here.
We show each example twice on this page. The first version of the example is shown in its entirety, next to a "Try
It!" button - we call this the 'read-only' code example, because it doesn't come from a database, and any changes
you make can't be posted back to the community. Feel free to copy the example code and paste it into your own programs.
The community postings follow, with "New" and "Reply" buttons added. As with the read-only code
example, you can run the code example, modify it and rerun it.
Unlike the read-only code examples, community postings allow you to create new discussion threads, or reply to
an existing posting. After you click on a 'Try It!' button in a community posting, or after you press a 'New' button
to start a new discussion thread,
edit your code, select the type of program (J2SE 6.0 Fragment or J2SE 6.0 Class), and then 'Run It!' until it works (or
you give up). When you click on the 'Save' button, the New Posting Wizard will appear. The wizard will prompt
you for information about your code example, and then save it to the database. Each posting contains
a code example, plus comments about the code. Next time you refresh this page your new posting will appear.
Learn more about Zamples and
play with samples for other APIs.
We would love to hear your feedback.
Visit the Mustang forum on java.net
Invoking JDK 6's new features
Go ahead and give it a try:
|
// Define a runnable Java class here. You must include a
// "main" method. Command line arguments go in the text area above.
public class Test {
public static void main(String argv[]) {
System.out.println("I love Zamples and J2SE 6!");
}
} |
Sample output:
I love Zamples and J2SE 6!
Correctly displaying the Java VM Version
Purpose: The previous posting had a common error - the pull-down menu that indicates the computer language was set to the default language (HTML/JSP).
Description: This posting correctly selects the "JDK 6 fragment" language, which means that the code example is not a complete class, just the contents of the main() method. |
System.out.println(System.getProperty("java.vm.version"));
|
Displaying the Java VM Version
Purpose: Discover the version of the Java VM.
Description: The system property java.vm.version returns the Java VM version. |
public class Test {
public static void main(String argv[]) {
System.out.println(System.getProperty("java.vm.version"));
}
}
|
JDK 1.6 early access
Purpose: We aren't sure what is in JDK 1.6 yet, and the "-source 1.6" option isn't working yet.
Description: Here is a simple code example that runs on Merlin (JDK 6.0 ea) |
// Define a runnable Java class here. You must include a
// "main" method. Command line arguments go in the text area above.
public class Test {
public static void main(String argv[]) {
System.out.println("I love Zamples and J2SE 6!");
}
}
|
|
| to add a new posting or reply to an existing posting. |
|
Add New Code Samples Here
If you have code samples that don't fit into any of the topics above which you would like to contribute,
please put them here.
We'll sort them out into their own sections as appropriate later.
List free space on each partition
Description: Presents the free space for each root in the filesystem, and the usable and total space allotted to the current user for each root in the filesystem. |
File[] roots = File.listRoots();
for (int i=0; i<roots.length; i++) {
System.out.println ("<b>File root " + roots[i] + "</b>");
System.out.println (" Free space = " + roots[i].getFreeSpace());
System.out.println (" Usable space = " + roots[i].getUsableSpace());
System.out.println (" Total space = " + roots[i].getTotalSpace());
}
|
|
| to add a new posting or reply to an existing posting. |
|
|