×
☰ See All Chapters

Multi-Catch Similar Exceptions

Java 7:

public class A {

        public void testMultiCatch() {

                try {

                        throw new FileNotFoundException("FileNotFoundException");

                } catch (FileNotFoundException | IOException fnfo) {

                        fnfo.printStackTrace();

                }

        }

}

Prior JDK7

public class A {

        public void testMultiCatch() {

                try {

                        throw new FileNotFoundException("FileNotFoundException");

                } catch (FileNotFoundException fnfo) {

                        fnfo.printStackTrace();

                } catch (IOException ioe) {

                        ioe.printStackTrace();

                }

        }

}

Diamond operator

Java 1.6 Syntax: Gen<Integer> iOb = new Gen<Integer>(88);

Java 1.7 Syntax:   Gen<Integer> iOb = new Gen(88);

Underscore Between Literals

You can place underscores between digits of any numeric literal like int, byte, short, float, long, double. Using underscores in numeric literals will allow you to divide them in groups for better readability.

public class A {

        public static void main(String[] args) {

                long ccNumber = 1234_5678_9012_3456L;

                long ssn = 999_99_9999L;

                float pi = 3.14_15F;

                long hexadecimalBytes = 0xFF_EC_DE_5E;

                long hexadecimalWords = 0xCAFE_BABE;

                long maxOfLong = 0x7fff_ffff_ffff_ffffL;

                byte byteInBinary = 0b0010_0101;

                long longInBinary = 0b11010010_01101001_10010100_10010010;

                int add = 12_3 + 3_2_1;

                System.out.println("ccNumber=" + ccNumber);

                System.out.println("ssn=" + ssn);

                System.out.println("pi=" + pi);

                System.out.println("hexadecimalBytes=" + hexadecimalBytes);

                System.out.println("hexadecimalWords=" + hexadecimalWords);

                System.out.println("maxOfLong=" + maxOfLong);

                System.out.println("byteInBinary=" + byteInBinary);

                System.out.println("longInBinary=" + longInBinary);

                System.out.println("add=" + add);

        }

}

Integer values in Binary format

Before java 1.7 it was possible to assign only octal and hexadecimal inter values. From java 1.7 we can assign binary equivalent of integer values. Binary values should start from 0b.

Java 7:

public class A {

        public static void main(String[] args) {

                int binary = 0b1000; //2^3 = 8

                if (binary == 8) {

                        System.out.println(true);

                } else {

                        System.out.println(false);

                }

        }

}

 

Prior JDK7

public class A {

        public static void main(String[] args) {

                int binary = 8;

                if (binary == 8) {

                        System.out.println(true);

                } else {

                        System.out.println(false);

                }

        }

}

 

 


All Chapters
Author