1

Below is the code:

class PlayWithBinary {
    static int age;

    public static void main (String args[]){
        int i = 0b10101010;
        System.out.println("my age is: " + age + " my salary is: "+ i);
    }
}

In terminal I executed : javac PlayWithBinary.java

For some reasons it is showing this error message:

PlayWithBinary.java:5: ';' expected
        int i = 0b10101010;
                 ^
1 error

Any ideas?

Update: for those who are getting similar errors, here is the link to download JDK 8 - Java SE Development Kit 8 Downloads

Devarshi
  • 16,440
  • 13
  • 72
  • 125

2 Answers2

3

Make sure your project is configured with a Java compliance level of Java 7 or above, or you could modify your code to use Integer.parseInt(String, int) like

int i = Integer.parseInt("10101010", 2);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

It sounds like you're using an older version of Java that doesn't support this. This feature was added in Java 7 - see the documentation and this related question. You should upgrade your Java version to fix this.

Alternatively, you can use Integer.parseInt(String, int) on your code: int i = Integer.parseInt("10101010", 2);

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86