Why Java Interprets 0123 as 83: A Subtle Syntax Trap

🔢 Why Does 0123 Print as 83 in Java? 🤔 While working on constructors today, I came across an interesting behavior in Java that reminded me how subtle details in syntax can completely change what your code does! When I wrote this line 👇 Student objectTwo = new Student(0123); I expected it to print 123. But instead, the console output was: 83 So what’s happening here? 💡 In Java, when a number starts with a leading zero (0), it is interpreted as an octal (base 8) number — not a decimal one. Let’s decode it: 0123 (octal) = 1×8² + 2×8¹ + 3×8⁰ = 64 + 16 + 3 = 83 (decimal) Hence, Java prints 83! --- 🧩 Takeaway: ✅ 123 → Decimal (Base 10) ✅ 0123 → Octal (Base 8) ✅ 0x123 → Hexadecimal (Base 16) ✅ 0b1010 → Binary (Base 2) --- 💬 Lesson: Tiny syntax details can make a big difference. Always watch out for leading zeros in numeric literals — they might silently convert your values to something unexpected! --- 🔖 #Java #ProgrammingTips #Developers #CodeLearning #JavaBasics #CodingCommunity #SoftwareEngineering #TechLearning

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories