Difference between int x= 10 and y=010 in Java.

Logically there is no difference between 10 and 010 in numbers. But in java, there is a difference of presentation and actual values of these numbers.

Int x = 10;

System.out.println(x);                      // Output: 10

Int y = 010;

System.out.println(y);                      // Output: 8

Exactly when we print 010 it actually prints 8 which is Octal value of digit 010.

You must learn about Octal base(8) number system before starting the code review.




Code Example:

/**
 * @author ashok.kumar
 *
 */
public class Test1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		int i=1;
		
		System.out.println("Numeric:" + i);
		
		int j=01;
		
		System.out.println("Numeric:" + j);
		
		int k=00;
		
		System.out.println("Numeric:" + k);
		
		int l=10;
		
		System.out.println("Numeric:" + l);
		
		
		int m=010;
		
		System.out.println("Octal value:" + m);
		
		int n=0100;
		
		System.out.println("Octal value:" + n);
		
                int o=0110;
		
		System.out.println("Octal value:" + o);
		
		int mn=1110;
		
		System.out.println("Numeric:" + mn);
		
		int jk=0112;
		
		System.out.println("Octal value: "+jk);
		
		int temp=0111;
		
		System.out.format("Display in Octal format: %04o",temp);	
	}
}

Output:
Numeric:1
Numeric:1
Numeric:0
Numeric:10
Octal value:8
Octal value:64
Octal value:72
Numeric:1110
Octal value: 74
Display in Octal format: 0111



1159total visits,4visits today

Leave a Reply

Your email address will not be published. Required fields are marked *