How do I define an integer constant in binary format?

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.basic;

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 result of our code snippet:

x = 42
y = 42

How do I read system property as an integer?

The Integer.getInteger() methods allows us to easily read system property and convert it 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.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", "2021");
        System.setProperty("app.minor.version", "9");

        // 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);
    }
}

The output of the code snippet:

App version = 2021.9

How do I decode string to integer?

The static Integer.decode() method can be used to convert a string representation of a number into an Integer object. Under the cover this method call the Integer.valueOf(String s, int radix).

The string can start with the optional negative sign followed with radix specified such as 0x, 0X, # for hexadecimal value, 0 (zero) for octal number.

package org.kodejava.lang;

public class IntegerDecode {
    public static void main(String[] args) {
        String decimal = "10"; // Decimal
        String hex = "0XFF"; // Hex
        String octal = "077"; // Octal

        Integer number = Integer.decode(decimal);
        System.out.println("String [" + decimal + "] = " + number);

        number = Integer.decode(hex);
        System.out.println("String [" + hex + "] = " + number);

        number = Integer.decode(octal);
        System.out.println("String [" + octal + "] = " + number);
    }
}

The result of the code snippet above:

String [10] = 10
String [0XFF] = 255
String [077] = 63

How do I convert string to an integer or number?

package org.kodejava.lang;

public class StringToInteger {
    public static void main(String[] args) {
        // Some random selected number, could representing a decimal,
        // hexadecimal or octal number.
        String myLuckyNumber = "13";

        // We convert a string to an integer by invoking parseInt() method
        // of the Integer class.
        int number = Integer.parseInt(myLuckyNumber);
        System.out.println("My lucky number is: " + number);

        // We can also converting a string representation of a number other
        // then the decimal base, for instance an hexadecimal by providing
        // the radix to the method.
        number = Integer.parseInt(myLuckyNumber, 16);
        System.out.println("My lucky number is: " + number);

        number = Integer.parseInt(myLuckyNumber, 8);
        System.out.println("My lucky number is: " + number);
    }
}

Our code results are:

My lucky number is: 13
My lucky number is: 19
My lucky number is: 11