Is 00 an integer or octal in Java? - java

Is
00, 000, or 000...
an integer in Java?
Or is it an octal? If is it an octal is
001 or 0005
an octal?

All are integers, but...
1 is decimal
0 is decimal
01 is octal
00 is octal
From Java Language Specification (emphasis mine):
Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral - not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.

Number literal representation examples in Java will give you the answer:
int decimal = 100;
int octal = 0144;
int hex = 0x64;
int binary = 0b1100100;
So 00, 000 and 0000 are all octal(base-8) integers.

Directly from specification
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.
You can check 0 though 7

Related

Whats the meaning of an error Integer number too large? [duplicate]

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

why does default value for char data type in java has 4 hex when it is a 16 bit datatype

I am new to java, and this question may be silly to many.
When going through the basics, i learnt this:
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
My question is why does the default, minimum and maximum have 4 hex when it can be only one?
Hex F is decimal 15 or binary 1111. It fits exactly in 4 bits. a 16-bit value can hold 4 times 4 bits, hence from 0x0000 to 0xFFFF (which is 2^16 = 65,536).
The \u in your example is for Unicode, pretty much saying that you can store unicode characters that take up to 16 bits, from \u0000 to \uFFFF.
I think you need to read up on numeral systems.
Binary: Represents numbers using 2 digits, 0 and 1.
Decimal: Represents numbers using 10 digits, 0 - 9.
Hexadecimal: Represents numbers using 16 digits, 0 - F.
A char in Java is a type that can hold numbers with 16 bits, i.e. in the range 0 - 1111111111111111 in binary, 0 - 65535 in decimal or 0 - FFFF in hexadecimal.

What does an integer that has zero in front of it mean and how can I print it?

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

Why "010" equals 8?

My simple question is why:
System.out.println(010|4);
prints "12"? I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number?
A leading 0 denotes an octal numeric value so the value 010 can be decoded thus: 010 = 1 * 81 + 0 * 80 = 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.
Now you should understand why 010 is 8.
That is because java takes it as an octal literal and hence produces 12. Try System.out.println(10|4) and the result is 14. Because this time it is taken as decimal literal.
As everybody mentioned here that 010 is an Octal Integer literal . The leading 0 specifies that it is an octal representation . Actual value will be :
1*8^1 + 0*8^0 = 8(decimal) = 1000(binary-only last 4 digits)
Now coming back to the SOP :
System.out.println(010|4);
Applying Bitwise OR on 010 and 4(considering only the last 4 digits) =>
1000|0100
= 1100
= 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 8 + 4 + 0 + 0
= 12(decimal)
Any number in Java which fulfill the below Conditions -
A. number Should have three or More Digital
B.Number should Start with 0.
If above Condition are true then number treated as Octal_Base (8) Number.
Therefore,
010=(8^2)*0+(8^1)*1+(8^0)*0=64*0+8*1+1*0=8
So,
010=8
One point you should consider that the number will be in octal if "0XX" i.e. both X are in range [0,7], otherwise it will result in "Integer number too large".

Why is 09 "too large" of an integer number? [duplicate]

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

Categories

Resources