Java SE 7 Feature: A Numeric Underscores with Binary Literals


Hello folks,

     In this tutorial, we will discuss about underscore ( _ ) in Literals feature added in Java SE7. Prior to Java SE 7, declaring numeric literals with underscores interspersed within the digits would causes a compile time error. But in JDK7, numeric literals with underscore characters are not only legal, but they’re highly encouraged to increase readability of code. This is a nice and small feature.

Underscore can be added anywhere in the value part with few constraints listed below,

Rules for using Numeric number with Underscores in Java 7
  1. Underscores ( _ ) can’t go at the beginning or the end of a number.
  2. You can’t use an underscore on either side of a decimal. 
  3. The underscore cannot go before an identifying suffix such as F, D or L
  4. You can’t put an underscore before or after the binary or hexadecimal identifiers b and x.
Example of valid literals:
long creditCardNumber
= 1234_5678_9012_3456L;
long socialSecurityNumber     
= 999_99_9999L;
float pi
= 3.14_15F;
long hexBytes
= 0xFF_EC_DE_5E;
long hexWords
= 0xCAFE_BABE;
long maxLong
= 0x7fff_ffff_ffff_ffffL;
byte nybbles
= 0b0010_0101;
long bytes
= 0b11010010_01101001_10010100_10010010;
Example of invalid literals with reason:
int a1     = _52;                             // This is an identifier, not a numeric literal
int a2     = 52_;                             // cannot put underscores at the end of a literal
int a3     = 0_x52;                         // cannot put underscores in the 0x radix prefix
int a4     = 0x_52;                         // cannot put underscores at the beginning of a number
float pi   = 3._1415F;                   // cannot put underscores adjacent to a decimal point
long ssn = 999_99_9999_L;        // cannot put underscores prior to an L suffix

In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number. This makes it easier to read code which uses bitwise operations. The following examples show binary literals:

int x = Integer.parseInt(“1000”, 2); //previously, you would have had to do it like this:

int eight = 0b1000; //in jdk7, you can create a binary literal like this:

int four = 0b1000>>1;      //easier to read bitwise operations

There is not limit in underscore. You can use as many underscore as you want. Only reason I see to use any number of underscores, is to be able to do fancy stuff like in the following piece of code (created by Joshua Bloch, if I’m not mistaken):

private static final int BOND =
     0000_____________0000________0000000000000000__000000000000000000+
   00000000_________00000000______000000000000000__0000000000000000000+
  000____000_______000____000_____000_______0000__00______0+
 000______000_____000______000_____________0000___00______0+
0000______0000___0000______0000___________0000_____0_____0+
0000______0000___0000______0000__________0000___________0+
0000______0000___0000______0000_________0000__0000000000+
0000______0000___0000______0000________0000+
 000______000_____000______000________0000+
  000____000_______000____000_______00000+
   00000000_________00000000_______0000000+
     0000_____________0000________000000007;

FEATURE SUMMARY:

Java numeric literals will allow underscores to be placed in (nearly) arbitrary positions within the number, at the programmer’s discretion, for readability purposes. These underscores shall be ignored by the compiler for the purposes of code generation.

MAJOR BENEFIT:

  1. Increased readability of code.

Advertisements

Java Versions, Features and History




On this “Java Version History” describes about the history of different java version.
From it’s inception, java language is undergone several changes.
From version 1.4, Java language development is undergone according to the rules of Java Community Process (JCP), and they uses Java Specification Requests(JSR) to propose and specify improvements and changes to the Java Language.



Java Versions :=


  • JDK 1.0 (1996, January 23), Codename Oak. [8 packages with 212 classes]
  • JDK 1.1 (1997, February 19) [23 packages with 504 classes]
  • J2SE 1.2 (1998, December 8), Codename Playground. [59 packages with 1520 classes]
  • J2SE 1.3 (2000, May 8 ), Codename Kestrel. [76 packages with 1842 classes]
  • J2SE 1.4 (2002, February 6), Codename Merlin. [135 packages with 2991 classes]
  • J2SE 5.0 (2004, September 30), Codename Tiger. [166 packages, over 3279 classes]
  • Java SE 6 (2006, December 11), Codename Mustang. [203 packages with 3793 classes]
  • Java SE 7 (2011, July 28), Codename Dolphin. [209 packages with 4024 classes]

Java Features :=

1. Java SE 7


    • [ Code named Dolphin ]

    • New features in Java SE 7

      • Strings in switch Statement
      • Type Inference for Generic Instance Creation
      • Multiple Exception Handling
      • Support for Dynamic Languages
      • Try with Resources
      • Java nio Package
      • Binary Literals, underscore in literals
      • Diamond Syntax
      • Automatic null Handling

      2. Java SE 6


      [Code named Mustang]

      • New features in Java SE 6

      • Scripting Language Support
      • JDBC 4.0 API
      • Java Compiler API
      • Pluggable Annotations
      • Native PKI, Java GSS, Kerberos and LDAP support.
      • Integrated Web Services.
      • Lot more enhancements.

      3. J2SE Version 5.0

      [Code named Tiger]

      • New features in Java SE 5.0

        • Generics
        • Enhanced for Loop
        • Autoboxing/Unboxing
        • Typesafe Enums
        • Varargs
        • Static Import
        • Metadata (Annotations)
        • Instrumentation


        4. J2SE Version 1.4

        [Code named Merlin]

        • New features in Java SE 1.4

            • XML Processing
            • Java Print Service
            • Logging API
            • Java Web Start
            • JDBC 3.0 API
            • Assertions
            • Preferences API
            • Chained Exception
            • IPv6 Support
            • Regular Expressions
            • Image I/O API

            5. J2SE Version 1.3

            [Code named Kestrel]

            • New features in Java SE 1.3

                  • Java Sound
                  • Jar Indexing
                  • A huge list of enhancements in almost all the java area.

                  6. J2SE Version 1.2

                  Code named Playground and released on December 8, 1998.

                  • New features in Java SE 1.2

                          • Collections framework.
                          • Java String memory map for constants.
                          • Just In Time (JIT) compiler.
                          • Jar Signer for signing Java ARchive (JAR) files.
                          • Policy Tool for granting access to system resources.
                          • Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.
                          • Java Plug-in
                          • Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.
                          • Audio support in Applets.

                          7. JDK Version 1.1

                          New features in Java SE 1.1

                                    • JDBC (Java Database Connectivity)
                                    • Inner Classes
                                    • Java Beans
                                    • RMI (Remote Method Invocation)
                                    • Reflection (introspection only)

                                    8. J2SE Version 1.0

                                    Codenamed Oak and released on January 23, 1996.

                                    Reference Link:=

                                    Avoiding browser popup for 401


                                    I was facing this issue recently, too. Since you can’t change the browser’s default behavior of showing the popup in case of a 401 (basic or digest authentication), there are two ways to fix this:


                                    • Change the server response to not return 401. Return a 200 code instead and handle this in your JQuery client.
                                    • Change the method that you’re using for authorization to a custom value in your header. Browsers will display the popup for Basic and Digest. You have to change this on both the client and the server.
                                    • headers : { “Authorization” : “BasicCustom” }
                                    • Alternatively, if you can customize your server response, you could return a 403 Forbidden.
                                    • The browser will not open the authentication popup and the jquery callback will be called.


                                    Please also take a look at.

                                    1. http://loudvchar.blogspot.in/2010/11/avoiding-browser-popup-for-401.html
                                    2. http://stackoverflow.com/questions/1297243/spring-security-how-to-mention-both-form-based-and-basic-authentication

                                    Top Java ebooks


                                    1. The Java Language Specification, Third Edition

                                    book cover
                                    Download : http://java.sun.com/docs/books/jls/
                                    Author : James Gosling, Bill Joy, Guy Steele, Gilad Bracha
                                    Description : Written by the inventors of the Java Language Specification. This book provides complete and detailed converge of the Java programing language.

                                    2. Thinking In Java, 3rd Edition

                                    book cover
                                    Download : http://www.mindviewinc.com/Books/downloads.html
                                    Author : Bruce Eckel
                                    Description : Great and recommended Java book, chapters and tutorials are posted, public review, correction.
                                    Note
                                    Thinking in Java, 3rd edition is still free, but you need to pay for the latest 4th edition.


                                    3. The Java Tutorial 4th Edition
                                    book cover
                                    Author : Sharon Zakhour, Scott Hommel, Jacob Royal, Isaac Rabinovitch, Tom Risser, Mark Hoeber
                                    Description : Accurate and up-to-date Java tutorials, and you can download the entire Java tutorials in bundle.

                                    4. Core Servlets And JavaServer Pages, Second Edition

                                    book cover
                                    Download : http://pdf.coreservlets.com/
                                    Author : Marty Hall and Larry Brown
                                    Description : Complete and detailed coverage of using Servlets and JavaServer Pages (JSP).

                                    5. Introduction To Programming Using Java, Sixth Edition

                                    book cover
                                    Download : http://math.hws.edu/javanotes/
                                    Author : David J. Eck
                                    Description : Good book for beginning programmers, and might also be useful for experienced programmers.

                                    6. Effective Java, Second Edition

                                    Effective Java (2nd Edition)
                                    Effective Java
                                    Author : Joshua Bloch