简体   繁体   English

预期的; Java中用二进制值初始化int变量时出错

[英]expected ; error when initializing an int variable with binary value in java

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 在终端中,我执行了: 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 更新:对于那些遇到类似错误的人,这里是下载JDK 8- Java SE Development Kit 8下载的链接。

确保您的项目配置了Java 7或更高级别的Java兼容级别,或者您可以修改代码以使用Integer.parseInt(String, int)

int i = Integer.parseInt("10101010", 2);

It sounds like you're using an older version of Java that doesn't support this. 听起来您正在使用的Java版本不支持此功能。 This feature was added in Java 7 - see the documentation and this related question . Java 7中已添加了此功能-请参阅文档相关问题 You should upgrade your Java version to fix this. 您应该升级Java版本以解决此问题。

Alternatively, you can use Integer.parseInt(String, int) on your code: int i = Integer.parseInt("10101010", 2); 或者,您可以在代码上使用Integer.parseInt(String, int)int i = Integer.parseInt("10101010", 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM