0

I am unable to understand the output of the below code

package beg;

import java.util.*;
import java.io.*;
public class Hell
{
    public static void main(String[] args)
    {
        System.out.println(10+010); //Prints 18
        System.out.println(010+010); //Prints 16
        System.out.println(010+10); //Prints 18
    }
}

Can somebody please explain?

Panther
  • 3,224
  • 9
  • 26
  • 48
Frosted Cupcake
  • 1,811
  • 2
  • 15
  • 32

2 Answers2

4

When you write 010 its octal code... not binary

System.out.println(10+010); 
System.out.println(010+010); 
System.out.println(010+10); 

These above lines are equivalent to:

System.out.println(10+8); // 010 being code for 8
System.out.println(8+8); 
System.out.println(8+10)
CoderNeji
  • 2,006
  • 3
  • 19
  • 31
0

This is not binary its summation of octal numbers. In java number started by 0 are octal. And output is in decimal.

010 = 8 .

System.out.println(10+010); 

10 + 8

System.out.println(010+010);

8 + 8

System.out.println(010+10); 

8 + 10

Panther
  • 3,224
  • 9
  • 26
  • 48