Tools

Tag: Integer

The JDK 7 add a small feature to work with a binary number. In the previous JDK we have to use the Integer.parseInt() method if we need to work with other base number. But with this new feature introduced in the Project Coin we can simplify the code when we work with the binary number.

To specify a binary literal in the code, add the prefix 0b or 0B to the number. The following code snippet show you how to write the binary literals:

package org.kodejava.example.fundamental;
public class BinaryLiteralExample {
public static void main(String[] args) {
// In JDK 6 and the previous version you must use the
// Integer.parseInt() method to define a number using
// a binary literal.
int x = Integer.parseInt("00101010", 2);
System.out.println("x = " + x);
// In the new JDK 7 you can simply use the following
// binary literal to define a number using a binary
// literal.
int y = 0b00101010;
System.out.println("y = " + y);
}
}

The Integer.getInteger() methods allows us to easily read system property and convert in directly to Integer object. This method call the System.getProperty() method and then convert it to integer by calling the Integer.decode() method.

package org.kodejava.example.lang;
public class IntegerProperty {
public static void main(String[] args) {
// Add properties to the system. In this example we create a
// dummy major and minor version for our application.
System.setProperty("app.major.version", "1");
System.setProperty("app.minor.version", "19");
// In the code below we use the Integer.getInteger() method to
// read our application version from the value specified in the
// system properties.
Integer major = Integer.getInteger("app.major.version");
Integer minor = Integer.getInteger("app.minor.version");
System.out.println("App version = " + major + "." + minor);
}
}