In Java, can I define an integer constant in binary format?

后端 未结 8 1001
[愿得一人]
[愿得一人] 2020-11-27 14:50

Similar to how you can define an integer constant in hexadecimal or octal, can I do it in binary?

I admit this is a really easy (and stupid) question. My google sea

相关标签:
8条回答
  • 2020-11-27 15:36

    In Java 7:

    int i = 0b10101010;
    

    There are no binary literals in older versions of Java (see other answers for alternatives).

    0 讨论(0)
  • 2020-11-27 15:38

    Search for "Java literals syntax" on Google and you come up with some entries.

    There is an octal syntax (prefix your number with 0), decimal syntax and hexadecimal syntax with a "0x" prefix. But no syntax for binary notation.

    Some examples:

    int i = 0xcafe ; // hexadecimal case
    int j = 045 ;    // octal case
    int l = 42 ;     // decimal case
    
    0 讨论(0)
提交回复
热议问题