This question already has answers here:
Closed 11 years ago.
They think it is:
Possible Duplicate:
Integer with leading zeroes
But if you check Integer with leading zeroes then you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.
I've a code:
class Test{
public static void main(String[] args){
int x=09;
System.out.println(x);
}
}
On compilation it gives an error: integer number too large : 09
Why it do so?
Again, if I change the code to:
class Test{
public static void main(String[] args){
int x=012;
System.out.println(x);
}
}
Now the output is 10
Why it give the output 10 instead of 12?
Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).
Hexadecimal literals begin with 0x, e.g. 0xA.
Up until Java 6, there was no literal notation for binary and
you'll had to use something like
int a = Integer.parseInt("1011011", 2);
where the second argument specifies the desired base.
Java 7 now has binary literals.
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
Integers beginning with the digit 0 are octal (base 8) numbers. The largest octal digit is 7; after 07 comes 010 (which is equal to decimal 8!)
012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.
09 is an octal numeric literal, an invalid one though.
Hexadecimal numbers start with 0x, like 0xFFFF.
There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.
Numbers beginning with 0 are octal number.
http://en.wikipedia.org/wiki/Octal
Related
This question already has answers here:
Closed 11 years ago.
They think it is:
Possible Duplicate:
Integer with leading zeroes
But if you check Integer with leading zeroes then you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.
I've a code:
class Test{
public static void main(String[] args){
int x=09;
System.out.println(x);
}
}
On compilation it gives an error: integer number too large : 09
Why it do so?
Again, if I change the code to:
class Test{
public static void main(String[] args){
int x=012;
System.out.println(x);
}
}
Now the output is 10
Why it give the output 10 instead of 12?
Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).
Hexadecimal literals begin with 0x, e.g. 0xA.
Up until Java 6, there was no literal notation for binary and
you'll had to use something like
int a = Integer.parseInt("1011011", 2);
where the second argument specifies the desired base.
Java 7 now has binary literals.
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
Integers beginning with the digit 0 are octal (base 8) numbers. The largest octal digit is 7; after 07 comes 010 (which is equal to decimal 8!)
012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.
09 is an octal numeric literal, an invalid one though.
Hexadecimal numbers start with 0x, like 0xFFFF.
There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.
Numbers beginning with 0 are octal number.
http://en.wikipedia.org/wiki/Octal
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 6 years ago.
See the following example:
int a = 0111;
System.out.println(a);
Output: 73
Why is this happening and how can i get the exact value without conversion?
According to the Java Language Specification 3.10.1. Integer Literals, there are 4 ways to write an integer literal:
DecimalNumeral: Digit 1-9 followed by zero or more digits 0-9, or the number 0.
HexNumeral: Leading 0x followed by one or more hexadecimal digits (0-9, a-f). Not case-sensitive.
OctalNumeral: Digit 0 followed by one or more digits 0-7.
BinaryNumeral: Leading 0b followed by one or more digits 0 or 1. Not case-sensitive.
(All 4 allow underscores for clarity)
As you can see, the number 111 depends on the prefix:
0111 (OctalNumeral) is 1*8*8 + 1*8 + 1 = 64+8+1 = 73.
111 (DecimalNumeral) is 1*10*10 + 1*10 + 1 = 100+10+1 = 111.
0b111 (BinaryNumeral) is 1*2*2 + 1*2 + 1 = 4+2+1 = 7.
0x111 (HexNumeral) is 1*16*16 + 1*16 + 1 = 256+16+1 = 273.
Which one is right depends on what you wanted.
Well you're getting the precise value. The problem is the format.
0111
gets interpreted as octal value in java. So your value actually is 73 as a quick calculation would show.
A octal value in java is defined this way:
OctalNumeral:
0 OctalDigits
0 Underscores OctalDigits
jls 3.10.1
So 0_111 would be interpreted in the same way.
The format to use in java code would be:
0b111
Which actually gets interpreted as 7 - in binary format - , as expected.
I'll add the links later on, I'm in a bit of a hurry.
class test{
public static void main(String args[]){
int a = 011;
System.out.println(a);
}
}
Why I am getting 9 as output instead of 011?
How can I get 011 as output?
The JLS 3.10.1 describes 4 ways to define integers.
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7 ...
A decimal numeral is either the single digit 0, representing the integer zero, or consists of an digit from 1 to 9 optionally followed by one or more digits from 0 to 9 ...
In summary if your integer literal (i.e. 011) starts with a 0, then java will assume it's an octal notation.
Solutions:
If you want your integer to hold the value 11, then don't be fancy, just assign 11. After all, the notation doesn't change anything to the value. I mean, from a mathematical point of view 11 = 011 = 11,0.
int a = 11;
The formatting only matters when you print it (or when you convert your int to a String).
String with3digits = String.format("%03d", a);
System.out.println(with3digits);
The formatter "%03d" is used to add leading zeroes.
Alternatively, you could do it in 1 line, using the printf method.
System.out.printf("%03d", a);
A numeric literal that starts with 0 is parsed as an octal number (i.e. radix 8). 011 is 9 in octal.
The 011 is being interpreted as an octal number. This means it's in base 8. See also this SO post. From the accepted answer by Stuart Cook:
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity.
Digits in Base 10:
100s, 10s, 1s
Digits in Base 8:
64s, 8s, 1s
So the 011 is interpreted as 8+1=9
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Why is 09 "too large" of an integer number? [duplicate]
(5 answers)
Closed 7 years ago.
i have an error while trying to insert 09 in my ArrayList's add method how to resolve it
when i google for it its give me solution like its a octal no and i need to chage this to decimal no
how do i do this please any one help me ...
import java.util.*;
class MyArrayList{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Integer(09));
System.out.println(al);
}
}
output:MyArrayList.java:5:error integer too large:09
Because values leading 0 are considered to be octal numbers. And octal numbers range from 0-7. if it considers your given value as octal , even though it is not valid octal number.
From the JavaDoc of the Integer constructor:
Constructs a newly allocated Integer object that represents the int
value indicated by the String parameter. The string is converted to an
int value in exactly the manner used by the parseInt method for radix
10.
So in case you wanted to insert 9 (base 10) with a leading 0, omit the '0'. If your number is indeed an octal number, create the Integer object by parsing:
Integer outputDecimal = Integer.parseInt(inputHex, 8);
This has nothing to do with the ArrayList. The problem is with 09 alone, for example a more characteristic example of the same problem:
int num = 09;
Because numeric literals starting with 0 are treated as octal numbers, and 9 is too big for that, as octal digits are from 0 to 7. So for the same reason 08 would give the same error, and 07 is fine.
Numbers starting with 0 in Java are considered as octal numbers and 9 is not an octal digit(only 0-7 are).
Numbers starting with 0x in Java are considered hexadecimal(the digits are 0-9 and A to F).
From Java SE 7, Numbers starting with 0b are considered binary(the digits are 0 and 1).
Hence your number 09 is recognised as octal and 9 doesn't exist in octal digits.
I understand this:
35%10 returns 5
but, why does 000000035%10 return 9?
Ruby does the same thing. I checked with irb.
Should I strip the 0s that are padding the number? What is the java function to do that?
When you put the leading zeros followed by octal digits (from 0 to 7), you're creating an octal literal. And since 000000035 in octal is 29 in decimal, its remainder with 10 is 9.
UPDATE
If you have your "octal literal" in a string, simply use Integer.parseInt and you'll get it parsed as a decimal.
assert Integer.parseInt("035") == 35;
Leading zero in number literal means octal number.
000000035 = 035 = 3*8 + 5 = 29
29 % 10 = 9
See this page for other available formats.