What is the Output of the below Program:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 08 of type int is out of range
at OcatalSample.main(OcatalSample.java:7)
Why??
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
Octal representation in java(base 8) :
An integer literal prefixed with 0 is treated as octal. It means 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.
In the above example #line7 gives an error, because as per above line range of octal is 0 to 7 only.
Note: Generally in octal representation, 7 + 1 is 10.
//OcatalSample.java public class OcatalSample{ public static void main(String []args){ int i = 012; int j = 034; int k = 056; int m = 08; System.out.println("i=" +i + ", j="+j+ ", k="+k+ ", m="+m); } }
public class ReverseNumber { public static void main(String[] args) { int number = 987; System.out.println("Reverse of given number is: "+ getReverseNumber(number)); } private static int getReverseNumber(int number) { int reverseNumber = 0; while (number > 0) { int reminder = number % 10; reverseNumber = reverseNumber * 10 + reminder; number = number / 10; } return reverseNumber; } } //Output: //Reverse of given number is: 789Output: It won’t compile. It gives below compilation error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The literal 08 of type int is out of range
at OcatalSample.main(OcatalSample.java:7)
Why??
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
Octal representation in java(base 8) :
An integer literal prefixed with 0 is treated as octal. It means 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.
In the above example #line7 gives an error, because as per above line range of octal is 0 to 7 only.
Note: Generally in octal representation, 7 + 1 is 10.