HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 7 / Underscores in numeric literals in Java 7

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

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 7 Tutorial

  • Java 7 – Feature Changes
  • Java 7 – Diamond Operator
  • Java 7 – String in Switch Statement
  • Java 7 – try-with-resources
  • Java 7 – Number Formatting
  • Java 7 – Suppressed Exceptions
  • Java 7 – Exception Handling
  • Java 7 – Fork/Join Framework
  • Java 7 – WatchService

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)