Underscores in numeric literals in Java 7

Java 7 has improved number literal formatting in very pleasant manner through allowing underscore in number. It makes reading number easy and when we write numeric literals in application logs, they look good.

For example, if we have to read a number “1000000“, then how much convenient it read in first sight. Not much, right??

We have a habit of reading numbers in 10,00,000 format. Good news is that  java has started supporting to write numbers in this format. Well, not exactly this but a matching format.

1. Underscores in numeric literals – example

After Java 7 release, now we can write above number like this : 10_00_000. Good enough, Isn’t it !!

A Java program for new formatted numbers with underscores is given below.

public class ImprovedNumbersInJava7
{
	@SuppressWarnings("unused")
	public static void main(String[] args)
	{
		/**
		 * Supported in int
		 * */
		int improvedInt = 10_00_000;

		/**
		 * Supported in float
		 * */
		float improvedFloat = 10_00_000f;

		/**
		 * Supported in long
		 * */
		float improvedLong = 10_00_000l;

		/**
		 * Supported in double
		 * */
		float improvedDouble = 10_00_000;
	}
}

2. Underscores in numeric literals – notes

  1. Underscores can be placed only between digits. We cannot put underscore next to decimal separator ‘dot’.
  2. Consecutive underscores are allowed. ’10___00′ is a valid number.
  3. To put underscore at the last of number is not allowed. ‘1000_’ is NOT a valid number. It will generate compile time error.
  4. In Java, underscore in front of variable name is allowed. Be careful, when you put a underscore in start of number. It a variable, not a number.

Happy Learning !!

Read More:

Java Docs

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.