× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

NetBeans 7 and Project Coin

Learn the changes in the Java language with help from the IDE

In a previous article, the new features introduced in version 7 of the NetBeans IDE were summarized, as well as the download and installation process. One of these new features was support to the modifications proposed by project Coin. In this article, we focus on this support.

Project Coin brings a set of new features to the Java programming language. These new features are incorporated into Java 7 and NetBeans 7 comes with support for such features. The support is active when the project's source/binary format is set to JDK 7, which should be the default. To verify, right-click on the project, select Properties and, in the section Sources, make sure the value of the field Souce/Binary Format is JDK 7, as you can see in Figure 1.

Figure 1. Project configuration to use the new features of JDK 7.

Project Coin was created with the goal of determining a set of small enhancements in the Java programming language to be proposed for Java 7 – the new version of the language. After discussion, the following new features were chosen: strings in switch; binary integral literals and underscores in numeric literals; improved type Inference for generic instance creation (diamond); multi-catch and more precise rethrow; try-with-resources statement; and simplified varargs method invocation.

The Java 7 proposal was registered as a Java Specification Request (JSR) at the JCP (Java Community Process) website, under the number JSR 336, which includes all the enhancements proposed to Java 7, including the ones suggested by Project Coin (JSR 334). In this article, we use examples that illustrate all the enhancements listed above, except for the last one in the list.

With this setting, the IDE will accept source codes that contain the modifications brought by the project Coin. A simple example is the code shown in Listing 1: the variable numBinary is assigned an integer literal, written in a binary base, while the variable numBig is assigned an integer literal, however this time the base is decimal with underscores separating every 3 digits to ease readability. In the case this program is executed, we will see that Java 7 assigns the value 7 to the first variable (111 on base-2 equals to 7 on base-10) and that the underscores are ignored during the assignment of the second variable.

Listing 1. Source code that uses binary literal and underscore in a numeric literal.

package br.com.javamagazine.nb7.coin;

public class NumericLiterals {
    public static void main(String[] args) {
        int numBinary = 0b111;
        int numBig = 1_234_567_890;

The NetBeans support to the modifications of Java 7 goes further than accepting the code on the new programming language structure. Another interesting functionality is the code refactoring, which can be written in a better fashion if such modifications would be used. For example, create in your project a class named StringsInSwitch and fill it with the content in Listing 2.

Listing 2. Source code that could get benefit of strings in switch.

package br.com.javamagazine.nb7.coin;

public class StringsInSwitch {
  public static void main(String[] args) {
    String[] months = new String[]{"January", "February", "March", "April"};

    for (String month : months) {
      System.out.println(month + " -> " + numDays(month));

  private static int numDays(String nameMonth) {
    if ("January".equals(nameMonth) || "March".equals(nameMonth))
      return 31;
    else if ("February".equals(nameMonth))
      return 28;
    else if ("April".equals(nameMonth))
      return 30;
      return -1;

At line 13, NetBeans highlights in yellow the if statement of the method numDays() (in boldface in the listing) and shows the icon of a light bulb in the left border of the editor, indicating that there is a hint to refactor the code at that point. By clicking at the light bulb (or pressing Alt+Enter with the cursor positioned at that line), a hint Convert to switch over Strings is shown, as you can see in Figure 2. After you click on the hint, NetBeans changes the content of the method numDays() to the source code shown in Listing 3.

Figure 2. Hints to refactor the source code and use switch over Strings.

Listing 3. Method numDays() after refactoring.

switch (nameMonth) {
  case "January":
  case "March":
    return 31;
  case "February":
    return 28;
  case "April":
    return 30;
    return -1;

Until version 6 of Java, such code would present compilation errors, because only discrete primitive types (integer, long, char, etc.) could be used in a switch command. From Java 7, however, it is possible to use also strings, and the switch is a better option than if and else if commands to compare strings using equals().
NetBeans provides hints for generic type inference, multiple exceptions in a catch and automatic resource management. To try the first one, you can use the code in Listing 4. For the other 2, you can create a class based on Listing 5.

Listing 4. Source code example that uses generic type inference.

List<Character> list = new ArrayList<Character>();
Map<Integer, String> map = new HashMap<Integer, String>();

list.add('A'); list.add('B'); list.add('C');
map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three");


Listing 5. Source code example for multible exceptions in a catch and automatic resource management.
package br.com.javamagazine.nb7.coin;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MultiCatchARM {
  public static void main(String[] args) throws Exception {
  private static void test () throws IOException {
    FileInputStream in = null;
    try {
      in = new FileInputStream("src/br/com/javamagazine/nb7/coin/MultiCatchARM.java");
      int k;
      while ((k = in.read()) != -1) {
    catch (FileNotFoundException e) {
    catch (IOException e) {
    finally {
      if (in != null) in.close();

On Listing 4, the first and second lines present the hint “Use diamond inference”. If you follow the hint provided by NetBeans, those lines will be modified in this way:

List<Character> lista = new ArrayList<>();
Map<Integer, String> mapa = new HashMap<>();

With generic type inference, given that we have declared the list and map types on the left side of the attribution, there is no need to declare them again on right side, during instance creation. It is sufficient to use the “diamond syntax”: <>. Java 7 infers automatically the generic type from the one that was previously declared.

In Listing 5, we find two hints. The first one is found on the definition of the variable in, in the first line of the method test(): Convert to try-with-resources. The second hint can be found on the first catch command: “Replace with multicatch”. Accepting the second hint, then in the sequence, accepting the first one, the method test() will be changed to the code you can see in Listing 6.

Listing 6. Method test() after refactoring, using ARM and multi-catch.

private static void test () throws IOException {
  try (FileInputStream in = new FileInputStream("src/br/com/javamagazine/nb7/coin/MultiCatchARM.java")) {
    int k;
    while ((k = in.read()) != -1) {

  catch (final FileNotFoundException | IOException e) {

At this point, we can also remove the declaration throws IOException from method test(), because it was only necessary when the method close() was called inside the block finally. Now, the closing of the resource in is managed automatically by Java 7. Furthermore, the two exceptions that before asked for two different catch command, now can be written in a single line using a multi-catch.

The listings showed in this section, especially Listing 5, where inspired in examples presented by a video in Arun Gupta’s blog (see Links).

Arun Gupta’s Blog (from Oracle), with a video showing the support of NetBeans 7 to project Coin.

Columnist not avaible

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login