how to set value of octal in java?

血红的双手。 提交于 2019-12-17 16:35:23

问题


I am trying to write following code.but it gives me error kindly help me.

    int six=06;
    int seven=07;
    int abc=018;
    int nine=011;
    System.out.println("Octal 011 ="+nine);
    System.out.println("octal O18 =" + abc);

why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable. Why this happen? what's the reason behind this Kindly tell me.
I got Following error

            integer number too large: 018
            int eight=018;

回答1:


Octal is base-8 number system, so it means digit can be from 0 to 7, you can't use digit 8 (and 9 too) in octal number system.




回答2:


why i cant give 018 and 019 to variable.

Because an integer literal prefixed with 0 is treated as octal, and '8' and '9' aren't valid octal digits.

From section 3.10.1 of the JLS:

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

Trying to use '8' in an octal number is like trying to use 'G' in hex... it's simple not part of the set of symbols used in that base.




回答3:


// Decimal declaration and possible chars are [0-9]
int decimal    =  495;        

// HexaDecimal declaration starts with 0X or 0x and possible chars are [0-9A-Fa-f]
int hexa       =  0X1EF; 

// Octal declaration starts with 0 and possible chars are [0-7] 
int octal      =  0757;  

// Binary representation starts with 0B or 0b and possible chars are [0-1]  
int binary     =  0b111101111; 

If the number is string format then you can convert it into int using the below

String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
                                  : Integer.decode(text);



回答4:


Octal numbers (base 8) can only use the following figures: 01234567. The same way that decimal numbers (base 10) can only use 0123456789.

So in octal representation, 17 + 1 is 20.




回答5:


why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.

The leading zero signifies an octal literal. However, 8 and 9 are not valid octal digits. This makes 018 and 019 invalid.




回答6:


The prefix 0 indicates octal(8 base)(digits 0-7).

public class MainClass{

  public static void main(String[] argv){

    int intValue = 034;  // 28 in decimal
    int six = 06; // Equal to decimal 6
    int seven = 07; // Equal to decimal 7
    int eight = 010; // Equal to decimal 8
    int nine = 011; // Equal to decimal 9

    System.out.println("Octal 010 = " + eight);

  }

}



回答7:


When an integer literal starts with 0 in Java, it's assumed to be in octal notation. The digits 8 and 9 are illegal in octal—the digits can range only between 0 and 7.




回答8:


Because it's octal, an octal number has 8 digits which spans from 0 to 7 inclusive. For the same reason 12 would be an invalid binary number.

You need at least base 9 to have 18 and a normal decimal base for 19.




回答9:


For your query.....you assign an invalid value to the variable....your assigned value is started with 0(zero) ..which means that you are assigning an octal value to the variable and when you assign a value higher then 7 such as 018 in your case...the value excedes the range of octal variables and hence show an error...so try entering simply 18 so it would take it as an integer rather than an octal variable data type...



来源:https://stackoverflow.com/questions/16433781/how-to-set-value-of-octal-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!