public class Test {
public static void main(String... args) {
int i=010;
System.out.print(i);
}
}
output:
8
Why? What is the logic?
0 is the prefix for octal numbers, just like 0x is the prefix for hexadecimal numbers (and 0b is the prefix for binary numbers, since Java 7).
So 010 means 1 * 81 + 0 * 80, which is 8.
Have a look at the Java Language Specification, Chapter 3.10.1 Integer Literals
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 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.
This is why 010 = 8.
Because 010, here is octal .
The integer is in octal-System because you have 0 at start.
Using 0 prefix means that you are using octal numbers.
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity.
If you write numbers with more than one significant digit you might be confused by the result.
// octal to decimal
01 == 1
02 == 2
07 == 7
010 == 8
020 == 16
024 == 20
// octal to binary (excluding most significant bit)
01 == 1
02 == 10
07 == 111
010 == 1000
020 == 10000
024 == 10100
Related
I'm trying to convert int's which start with 0 to strings to be stored in a phone directory as the telephone numbers can start with 0.
I've tried -
int num = 0125;
String.format("%04d",num);
and
Integer.toString(num);
and
DecimalFormat df = new DecimalFormat("0000");
df.format(num);
Each time I get the output 0085 rather than 0125.
How do I convert an int with a leading zero to a string in decimal format?
An int value starting with a zero is considered to be a octal number (having numbers from 0 - 7) similar to hexadecimal numbers. Hence your value:
0125
is equal to: 1 * 82 + 2 * 81 + 5 * 80 == 64 + 16 + 5 == 85
Don't try to represent a phone-number as an int. Instead use a String and validate it using a regex expression. If you combine both, you may as well represent a phone number by its own type like:
public class PhoneNumber {
private final String number;
public PhoneNumber(String number) {
if (number == null || !number.matches("\\d+([-]\\d+)?")) {
throw new .....
}
this.number = number;
}
}
The regex is just an example matching phone numbers like: 1234 or 0123-45678.
A numeric literal that starts with 0 is considered to be Octal (base 8). 125 base 8 is 85 base 10 (decimal).
Also, int i = 09 will throw a compiler error for the same reason.
See 09 is not recognized where as 9 is recognized
0125 is actually 85. Why?
Numbers that starts with 0, are octal numbers. So 0125 is:
5*80 + 2*81 + 1*82 = 85
See the JLS - 3.10.1. Integer Literals:
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.
This question already has answers here:
why is not (123 == 0123) in java?
(3 answers)
Closed 9 years ago.
I am confused why this is and I cannot seem to find an answer why. This is from the assignment:
x=1, y=2, z=3;
z=(int)(x/y*3.0+z*012);
System.out.printf("%d %d %d", x, y, z);
Answer is :
1 2 30; << from eclipse
How I arrived here:
(1/2) = 0 * 3.0 = 0 + (z*012)= 30. I wanted to say 36 but I guess it is 30 according to the IDE.
012 is octal number not decimal which decimal value is 10.
z=(int)(x/y*3.0+z*012);
is equals -
z=(int)(1/2*3.0+3*10);
For reference
Numeric starts with 0 is octal number.
Numeric starts with 0x is hexadecimal number.
Numeric starts with 0b or OB is binary number.(Since Java edition 7 - Binary Literals)
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity. Here 012 is an octal number which has a decimal value f 10
So your multiplication will come like
z = (int) (1/2 * 3.0 + 3 * 10);
From 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.
012 is an octal, because it starts with 0:
012 = (0 * 8^2) + (1 * 8^1) + (2) = 10
Therefore:
012 * 3 = 10 * 3 = 30
Notes:
Remember that an octal is a number in base 8 (decimal is base 10), so it can't have digits larger or equal to 8.
Similarly, hexadecimal numbers starts with 0x, for example: 0x12 = 1*16 + 2 = 18
See 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.
So,
012 = 0 * 82 + 1 * 81 + 2 * 80 = 10
In Java 7, you can use underscores in numeric literals which might help you interrupting the value.
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;
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.
// 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);
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.
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.
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);
}
}
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.
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.
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.
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...
I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third "if" block is not reachable. Why?
When I add a leading zero to a number, the equal operator returns false.
int var = 123;
if (var == 0123) {
//not reachable
}
if (var == 123) {
//reachable
}
if (var == (int)0123) {
//not reachable
}
if (var == (int)123) {
//reachable
}
0123 is an octal number (leading 0), while 123 is a decimal number.
so 0123 actually equals to 83.
Any integer Number Leading With Zero is octal Number (base 8).
0123 is octal Number and 123 is Decimal Number
0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)
=3+16+64+0
=83
because 0123 in not decimal digit its octal (base 8)
so this is equal to 83
To convert a number k to decimal, use the formula that defines its base-8 representation:
0123 base-8 = 83 decimal
0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)
=3+16+64+0
=83
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
Note: Octal values are denoted in java by leading zero normal decimal number cannot have a leading zero
This prints 83
System.out.println(0123)
However this prints 123
System.out.println(123)
Why does it work that way?
A leading zero denotes that the literal is expressed using octal (a base-8 number).
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3), which equals 83 in decimal.
For some reason, octal floats are not available.
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
There is also a 0x prefix which denotes that the literal is expressed in hexadecimal (base 16).
Because integer literals starting with 0 are treated as octal numbers.
See section 3.10.1 of the JLS
Try this:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C.
For comparison, try
System.out.println(0x123);
In Java integer literals with a leading zero are octal integers (base 8).
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L and 0101L are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
Even as per the Regex patterns in Oracle docs,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
Thus, your number 0101 be it in Integer or Long format is treated as an Octal representation of a number.
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65
printf will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d" if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d", "%012d", etc...