14

In my actual project It happened accidentally here is my modified small program.

I can't figure out why it is giving output 10?

public class Int
{
    public static void main(String args[])
    {
        int j=012;//accidentaly i put zero 
        System.out.println(j);// prints 10??
    }
}

After that, I put two zeros still giving output 10.

Then I change 012 to 0123 and now it is giving output 83?

Can anyone explain why?

user3463521
  • 572
  • 2
  • 7
  • 16
akash
  • 22,664
  • 11
  • 59
  • 87
  • 3
    `1` is decimal, `01` is octal, `0x1` is hexadecimal, `0b1` is binary (in [Java SE 7](http://docs.oracle.com/javase/7/docs/technotes/guides/language/binary-literals.html)) Edit: `'1'` is char, `"1"` is string. – Grijesh Chauhan Apr 13 '14 at 09:45
  • Additionally, you may like to know that `08` is [compilation error](http://stackoverflow.com/questions/16433781/how-to-set-value-of-octal-in-java) – Grijesh Chauhan Apr 13 '14 at 09:51
  • @GrijeshChauhan ThankYou for your reply.I would rather accrepted your comment as an answer. :) your explanation is perfect and +1 for link. – akash Apr 13 '14 at 10:13
  • Because there's no decimal literal (base-10) prefix in Java, you can't start a decimal value with a leading zero. – buchWyrm May 08 '19 at 20:56
  • it happened on kotlin. I create a class with constructor on it called class codeCollection(var code:Int = 023){} Int was giving me error!!. Like ")" is missing. Now I know what's the problem was. – sophin Jul 08 '20 at 07:48

5 Answers5

26

Than I change 012 to 0123 and now it is giving output 83?

Because, it's taken as octal base (8), since that numeral have 0 in leading. So, it's corresponding decimal value is 10.

012 :

(2 * 8 ^ 0) + (1 * 8 ^ 1) = 10

0123 :

(3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • thanks for this, how would I determine whether it's taken as octal base or some other base? – Bhavesh Agarwal Oct 01 '14 at 14:09
  • @BhaveshAgarwal : It's clearly explained, since that numeral have 0 in leading, it's considerder as octal base. – Abimaran Kugathasan Oct 02 '14 at 04:22
  • thanks Abimaran. Sorry for not being clear on my query. I intend to ask how would we "in general" determine whether a number is in which base. Like numbers starting from 0 will be octal, with "x" will be decimal, with "y" will be blah blah... Just wanted to understand the basics identifying the bases, for all important/famous bases. – Bhavesh Agarwal Oct 03 '14 at 04:43
  • Just to make it clear for some people trying to calculate the operation above, there should be: 3 * 8 ^ 0 + 2 * 8 ^ 1 + 1 * 8 ^ **2** – intersum Oct 27 '14 at 13:28
  • @BhaveshAgarwal if I understand you correctly you asking literal prefixes for numbers. Java supports octal, hexadecimal, decimal and binary. We can represent an `Octal` number by adding the `0 prefix` to it. `Hexadecimal` number by adding the `0x prefix` to it. we `don’t add any prefix`, the number is treated as a `decimal` number. And with Java 7 we have a new literal prefix to represent `binary base`, which is the `0b prefix`. Read: http://rodrigosasaki.com/2013/06/10/number-literals-in-java/ – Govinnage Rasika Perera Jun 03 '15 at 05:01
6

The leading zero means the number is being interpreted as octal rather than decimal.

Mark
  • 2,792
  • 2
  • 18
  • 31
3

If a number is leading with 0, the number is interpreted as an octal number, not decimal. Octal values are base 8, decimal base 10.

System.out.println(012):
(2 * 8 ^ 0) + (1 * 8 ^ 1) = 10
12 = 2*8^0 + 1*8^1 ---> 10


System.out.println(0123)
(3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83
123 = 3*8^0 + 2*8^1 + 1*8^2 ---> 83
F. Müller
  • 3,969
  • 8
  • 38
  • 49
Amit
  • 72
  • 4
2

You are assigning a constant to a variable using an octal representation of an type int constant. So the compiler gets the integer value out of the octal representation 010 by converting it to the decimal representation using this algorithm 0*8^0 + 1+8^1 = 10 and then assign j to 10. Remember when you see a constant starting with 0 it's an integer in octal representation. i.e. 0111 is not 1 hundred and 11 but it's 1*8^0 + 1*8^1 + 1*8^2.

user3463521
  • 572
  • 2
  • 7
  • 16
1

By placing a zero in front of the number is an integer in octal form. 010 is in octal form so its value is 8

Guess output of this program on your own :

 public class sol{
       public static void main(String[] args){
             int p = 010;
             int q = 06;
             System.out.println(p);
             System.out.println(q);
       }
 }

output is :

8
6
Monu Rohilla
  • 1,311
  • 5
  • 20