Saturday 12, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 28 December 2013

Java7 – Underscores in Numeric Literals

In this tutorial we are going to see underscores in numerical literals.
Note: In this tutorial some of code taken from Oracle official web site
In Java7 any number of underscore character (_) can appear in anywhere between numerical literals.The underscore is only present in the representation of literals in Java code, it will not appear if you print the literal value.
Example
int i = 1000_1000;
Output
10001000
Examples to use underscore between numerical liters.
1.long creditCardNumber = 1234_5678_9012_3456L;
2.long socialSecurityNumber = 999_99_9999L;
3.float pi =  3.14_15F;
4.long hexBytes = 0xFF_EC_DE_5E;
5.long hexWords = 0xCAFE_BABE;
6.long maxLong = 0x7fff_ffff_ffff_ffffL;
7.byte nybbles = 0b0010_0101;
8.long bytes = 0b11010010_01101001_10010100_10010010;

Rules to use underscores between literals

  1. Place the underscore only between numerical literals

  2. you cannot place the underscore in the following places
  • At beginning or end of number.

  • Adjective to decimal point in a floating point literal.

  • Prior to L or F suffix

  • In position where String of digits is expected
Example of valid and invalid underscore placement given below

float pi1 = 3_.1415F;      // Invalid; cannot put underscores adjacent to a decimal point

float pi2 = 3._1415F;      // Invalid; cannot put underscores adjacent to a decimal point

long socialSecurityNumber1 = 999_99_9999_L;     // Invalid; cannot put underscores prior to an L suffix  
int x1 = _52;              // This is an identifier, not a numeric literal

int x2 = 5_2;              // OK (decimal literal)

int x3 = 52_;              // Invalid; cannot put underscores at the end of a literal

int x4 = 5_______2;        // OK (decimal literal)

int x5 = 0_x52;            // Invalid; cannot put underscores in the 0x radix prefix

int x6 = 0x_52;            // Invalid; cannot put underscores at the beginning of a number

int x7 = 0x5_2;            // OK (hexadecimal literal)

int x8 = 0x52_;            // Invalid; cannot put underscores at the end of a number

int x9 = 0_52;             // OK (octal literal)

int x10 = 05_2;            // OK (octal literal)

int x11 = 052_;            // Invalid; cannot put underscores at the end of a number

Note: In this tutorial some of code taken from Oracle official web site

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java7 – More precise Re-throw of an ExceptionIn this tutorial we are going to see re-throw exception in Java7.In previous versions of Java re-throwing exception in catch block didn’t indicate the actual exceptions possible from the try block.Also you could not change th… Read More
  • Java7 Features In this Tutorials we are going to see what are the changes and new features comes in Java7. Virtual Machine : JSR 292: Support for dynamically-typed languages (InvokeDynamic) Extensions to the JVM, the Java language, … Read More
  • Java7 – Underscores in Numeric LiteralsIn this tutorial we are going to see underscores in numerical literals.Note: In this tutorial some of code taken from Oracle official web siteIn Java7 any number of underscore character (_) can appear in anywhere between nume… Read More
  • Java7 – Binary LiteralsIn this tutorials we are going to see the integral types (byte, short, int, and long ) in  java7.This tutorials is created by referring Oracle official web site.Some content is taken from there.  In Java7, the integ… Read More
  • Java 7 – Switch statement on StringIn this tutorials we are going to see old switch statement and new switch statement in java which support String argument. Previous versions of java was not supported  String in switch statement.To achieve this function… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Java7 – Underscores in Numeric Literals Rating: 5 Reviewed By: eHowToNow