How a zero can change everything in Java (a puzzler)

How a zero can change everything in Java (a puzzler)

Java is a popular and widely used programming language, but it also has some quirks and surprises that can puzzle even experienced developers. In this blog post, we will explore one such puzzler: what does the following code print?

public class Puzzler {
    public static void main(String[] args) {
        int x = 010;
        System.out.println(x);
    }
}

Reveal answer

If you run this code, you will see that it prints 8 to the standard output. But why? Shouldn’t it print 10, since that is the value we assigned to the variable x?









Seamlessly use Github using SSH keys
GitHub, along with many other code versioning platforms (such as Bitbucket), provides a secure and efficient way to manage and collaborate on projects. By utilizing SSH keys, you can seamlessly interact with your repositories, enabling secure authentication without the need for passwords. By leveraging SSH’s config file, you can simplify

If an integer literal starts with a zero, it is treated as an octal number


The answer lies in the way Java interprets integer literals. A literal is a fixed value that is written directly in the code, such as 10true, or "Hello". Java has different rules for different types of literals, and one of them is that if an integer literal starts with a zero, it is treated as an octal number.

Octal is a base-8 number system, which means it uses eight digits from 0 to 7. To convert an octal number to a decimal number, we need to multiply each digit by a power of 8, starting from the rightmost digit.

For example, the octal number 010 is equivalent to the decimal number 1 * 8^1 + 0 * 8^0 = 8 + 0 = 8.

What if you use parseInt?

Let's try it! Let's see the code below:

public class Puzzler {
    public static void main(String[] args) {
        int x = Integer.parseInt("010");
        System.out.println(x);
    }
}

Integer's parseInt method strips out any leading zeroes before parsing, and outputs a 10.

Conclusion

To avoid this confusion, we can use decimal literals (without leading zeros) for integer values, unless we explicitly want to use octal, hexadecimal, or binary literals.

This puzzler is a good example of how Java can sometimes behave in unexpected ways, and why we should always be careful and attentive when writing and reading code.

If you'd like to learn more useful to know puzzlers, check them out below! 😄

Do you know this Java puzzler about missing breaks in switches?
Java is a popular programming language that has many features and quirks. One of them is the switch statement, which allows you to execute different blocks of code based on the value of an expression. For example, you can use a switch statement to print a message depending on the

Things you might like

Beginner’s guide to the Linux terminal. Everything you need to know in under 8 minutes of your time
Linux is an open-source operating system that powers many servers, supercomputers, and devices. Learning Linux can boost your career as a software developer, system administrator, cybersecurity professional, or IT support specialist. In this blog post, I will introduce one of the most useful skills you need to master in Linux:
11 Things You Can Do to Secure Your Linux Server
Linux is one of the most popular and widely used operating systems in the world, especially for servers. Linux servers power millions of websites, applications, databases, and other services that we use every day. However, Linux servers are not immune to cyberattacks, and they require proper security measures to protect
grep: Do you know this must-have skill you can learn in just a few minutes?
If you’ve recently switched to Linux or even MacOS, you’ve likely encountered at least some degree of terminal commands. Don’t let the command line intimidate you – things get rather spicy if you know it well. Let’s explore a simple yet incredibly useful command: grep. Introducing grep
Practical guide on using the LESS command in Linux
The less command tends to become one of the most useful Linux commands for various reasons. It’s definitely one of mine - I use it at work for log scanning and debugging quite a bit. Knowing your tools is key, so let’s dive into this neat tool further! 🐧 The less