Java Puzzle

Question:

 class Puzzle {  
      public static void main(String[] args) {  
           System.out.print(12345 + 5432l);  
           System.out.print(" ");  
           System.out.print(01234 + 43210);  
      }  
 }  
 Output :  
 17777 43878  

In lines:
System.out.print(12345 + 5432l);  
5432l ---- Its last character is L -- means long

System.out.print(01234 + 43210);  
01234 ---- Here starting digit is 0 which means octal

Fixed version :
 class Puzzle {  
      public static void main(String[] args) {  
           System.out.print(12345 + 5432L); // make the last character L  
           System.out.print(" ");  
           System.out.print(1234 + 43210); // drop the 0  
      }  
 }  
 Output :  
 66666 44444  

Comments

Popular posts from this blog

Comparison of Cloud Services

When to use Import-Package and Require-Bundle in Eclipse Plugin?