ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Java 7 new feature - Using underscores in literals to improve code readability.

Updated on September 17, 2014

Introduction -

Java is a very powerful object oriented programming language. Java 7 has come up with very new and important features. In this tutorial, you will learn Java 7 new feature - how to use underscores in literals. In order to improve code readability Java 7 came with a feature to use underscores in literals. With release of Java 7, numerical literals can have (_) underscore characters. Underscores can be used in numerical literals at any place to make a grouping between significant digits. It gave freedom to Java programmers to use underscores in numerical literals at any digit. In Mathematics, there are 4 number system such as, decimal, hexadecimal, octal and binary etc, this underscores can be applied to all these systems. Underscores can be applied to primitive data types such as integer and floating point literals. The real world examples would be to use underscores in debit and credit card numbers. For example - 8736_8738_0623_0790.

Java 7 new feature – Using underscore in literals to improve code readability.
Java 7 new feature – Using underscore in literals to improve code readability.

Java source code - Java 7 new feature - Using underscore in literals - Eclipse Snapshot

Java source code - Java 7 new feature - Using underscore in literals - Eclipse Snapshot
Java source code - Java 7 new feature - Using underscore in literals - Eclipse Snapshot

Java source code - Java 7 new feature - Using underscore in literals.

// Create a Java class by name "UnderscoresInLiterals".
public class UnderscoresInLiterals {

	// Create a main method. It will be starting point of execution.
	public static void main(String[] args) {

		// Prior to Java 7, credit card and debit card numbers were
		// defined as below. It usually demonstrated poor code 
		// readability. The programmers faced difficulty in reading 
		// long numbers such as, Credit Card numbers and Debit Card numbers.
		long creditCardNumber = 7643243509361521L;
		long debitCardNumber = 7349216509381622L;

		// Lets print on the Java console the long numbers.
		System.out.println("The Credit Card Number (without literals) is - " + creditCardNumber);
		System.out.println("The Debit Card Number (without literals) is - " + debitCardNumber);

		// With Java 7, now programmers can use underscores in numerical literals
		// as shown below. It improved code readability. Programmers can use underscores
		// at the position to separate significant groups. Here, Credit Card and Debit Card 
		// numbers have significant groups of 4.
		creditCardNumber = 7643_2435_0936_1521L;
		debitCardNumber = 7349_2165_0938_1622L;

		// Printing above credit card and debit card numbers would produce same result.
		System.out.println("The Credit Card Number (with literals) is - " + creditCardNumber);
		System.out.println("The Debit Card Number (with literals) is - " + debitCardNumber);

		// Underscore usage in Integer types
		int number = 235536;
		System.out.println("The integer data type (without literals) is - " + number);

		// Below value of number variable makes it easy to classify that value
		// of integer would be 2,35,536.
		number = 2_35_536;
		System.out.println("The integer data type (with literals) is - " + number);

		// Underscore usage in floating point types
		float amount = 5342.03F;
		System.out.println("The float data type (without literals) is - " + amount);

		amount = 5_342.03F;
		System.out.println("The float data type (with literals) is - " + amount);

		// Underscore usage in Hexadecimal numbers.
		int underscoreWithoutHex = 0xA6;
		System.out.println("The  hexadecimal number (without literals) is - " + underscoreWithoutHex);

		int underscoreInHex = 0xA_6;
		System.out.println("The  hexadecimal number (with literals) is - " + underscoreInHex);

		// Underscore usage in Binary numbers.
		int underscoreWithoutBinary = 0b10110001;
		System.out.println("The  binary number (without literals) is - " + underscoreWithoutBinary);
		
		int underscoreInBinary = 0b1_011_0001;	
		System.out.println("The  binary number (with literals) is - " + underscoreInBinary);

	}

}

Underscore usage in integral data types.

Underscore can be used effectively with integral data types such as, byte, short, int, long, char etc. The demo code is shown below. The code demonstrate how to use underscores in integral datatypes. Underscores in Integral datatypes makes code more readable.

                // Underscores in byte datatypes	
		byte b = 1_8;
		// Underscores in short datatypes	
		short s = 3246_7;
		// Underscores in integer datatypes	
		int i = 4563_78;
		// Underscores in long datatypes	
		long l = 64560_3534_3L;
		// Underscores in character datatypes	
		char c = 12_5;

Underscore usage in floating point data types.

Underscore can be used effectively with floating point data types such as, float and double etc. The demo code is shown below. The code demonstrate how to use underscores in floating point datatypes. Underscores in floating point datatypes makes code more readable.

                // Underscores in float datatypes
		float f = 3.4_56f;
		// Underscores in double datatypes
		double d = 345_4343.7869D;

Underscore usage in hexadecimal, binary and octal numbers.

Underscore can be used effectively with hexadecimal, binary and octal numbers etc. The demo code is shown below. The code demonstrate how to use underscores in hexadecimal, binary and octal numbers. Underscores in hexadecimal, binary and octal numbers makes code more readable.

                // Underscores in hexadecimal
		int hexadecimal = 0xA_6;
		// Underscores in octal
		int octal = 01_2;
		// Underscores in binary
		int binary = 0b1_011_0001;

Output of the program - Using underscores in literals - Eclipse Snapshot

Output of the program - Using underscores in literals - Eclipse Snapshot
Output of the program - Using underscores in literals - Eclipse Snapshot

Output of the program -

The Credit Card Number (without literals) is - 7643243509361521
The Debit Card Number (without literals) is - 7349216509381622
The Credit Card Number (with literals) is - 7643243509361521
The Debit Card Number (with literals) is - 7349216509381622
The integer data type (without literals) is - 235536
The integer data type (with literals) is - 235536
The float data type (without literals) is - 5342.03
The float data type (with literals) is - 5342.03
The hexadecimal number (without literals) is - 166
The hexadecimal number (with literals) is - 166
The binary number (without literals) is - 177
The binary number (with literals) is - 177

Invalid usages of underscore in literals

public class InvalidUnderscoresInLiterals {
	
	public static void main(String[] args) {

		// Underscore cannot come at the start of integer value
		int number = _1234;  // compilation error.
		
		// Underscore cannot come before or after decimal point
		float floatNumber = 43_.34; // compilation error.
		
		// Underscore cannot come before any primitive data type literal
		// such as l, L, d, D etc.
		long longNumber = 56_374_L; // Compilation error.
	}
}

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article