NetBeans IDE 7.0 introduces support for new JDK 7 language features, such as the diamond operator, strings in switch, multicatch, etc. When you use these constructs in your code, the IDE recognizes them, offers correct classes in code completion, correctly highlights errors, and lets you automatically fix syntax. Thus, NetBeans IDE 7.0 helps you write code that is compatible with the JDK 7 language specification.
In this tutorial, you learn how to enable JDK 7 support in the IDE and see how the IDE handles new language constructs.
Contents
To complete this tutorial, you need the software and resources listeds in the following table.
Software or Resource | Version Required |
---|---|
NetBeans IDE | version 7.0 |
Java Development Kit (JDK) | version 7 |
Before the official release of Java™ Platform, Standard Edition 7, you can obtain the JDK 7 source code and development builds from the OpenJDK Project website.
To enable JDK 7 support in the NetBeans IDE:
Click here to view full-sized, Ctrl-Click to download, 1,1 MB. |
Note: The default Platform Javadoc API location is this web URL. For offline use, you can download the javadoc JAR file from the JDK7 binary snapshots page and specify its location on your system.
Once you have registered JDK 7 in the IDE, you need to configure your project to use this JDK for compilation, running, and debugging.
Click here to view full-sized, Ctrl-Click to download, 1,3 MB. |
JDK 7 brings a number of new features and enhancements in different areas, including internationalization, I/O and networking, security, etc. The best way to illustrate the JDK 7 support by the IDE's Java Editor is to demonstrate a few language changes introduced by Project Coin.
One of these changes is a "String in a switch". In the previous versions of Java, the argument of switch had to be only of the following primitive data types: byte, short, char, int, or enum. Starting from JDK 7, you can use arguments of type String in the expression of a switch statement.
package switchtest; public class SwitchTest { public static void main(String[] args) { String color = "red"; String colorRGB; switch (color.toLowerCase()) { case "black": colorRGB = "000000"; break; case "red": colorRGB = "ff0000"; break; case "green": colorRGB = "008000"; break; case "blue": colorRGB = "0000ff"; break; default: colorRGB = "Invalid color"; break; } System.out.println(colorRGB); } }
If the pasted code is formatted incorrectly in the editor, press Alt-Shift-F to reformat.
Click here to view full-sized, Ctrl-Click to download, 2,5 MB. |
To demonstrate how the IDE's Java Editor recognizes and automatically fixes code to be compliant with the JDK 7 language spec, let's use a dummy code snippet, which is meaningless but contains all the major language improvements.
When walking through this dummy code snippet and applying editor hints, you will see the following examples of how to:
package switchtest; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class SwitchTest { public static void main(String[] args) throws IOException { List<String> list = new ArrayList<String>(); HashMap<String, Integer> map = new HashMap<String, Integer>(); HashMap<String, Integer> map2 = new HashMap<String, Integer>(); String a = "ehlo"; try { throw new FileNotFoundException("adasdf"); } catch (FileNotFoundException fnfo) { fnfo.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } FileInputStream in = null; try { in = new FileInputStream("foo.txt"); int k; while ((k = in.read()) != -1) { System.out.write(k); } } finally { if (in != null) { in.close(); } } } }
Click here to view full-sized, Ctrl-Click to download, 1,8 MB. |
For more information about JDK 7 and the NetBeans IDE, see:
For more information about developing Java applications in the NetBeans IDE, see: