Java (JDK) 7 New Features: 1. Binary Literals

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

JAVA (JDK) 7 NEW FEATURES


1. Binary Literals
2. Multi-Catch Exceptions
3. Numerics with Underscore
4. Switch Case-String Type
5. Try With Resource (AutoCloseable)
6. Type Inference In Generics

1. Binary Literals :-
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. The following examples show binary literals:

package com.app7;

public class BinaryLiteralsTest {

public static void main(String[] args) {


int sid=0b10; //0b or 0B
System.out.println(sid);

int binyear=0b11111100010;
System.out.println(binyear);

//int to binary
int year=2018;
String binstr=Integer.toBinaryString(year);
System.out.println(binstr);
}
}

1
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

2. Multi Catch Exception:-


In Java SE 7 and later, a single catch block can handle more than one type of
exception. This feature can reduce code duplication and lessen the temptation
to catch an overly broad exception.

package com.app7;

public class MultiCatchException {

public static void main1(String[] args) {

try {
int a[]=new int[10];
a[11]=6/0;
} catch(ArithmeticException |ArrayIndexOutOfBoundsException ae) {
System.out.println(ae.getMessage());
}catch(Exception e) {
System.out.println(e);
}

public static void main(String[] args) {


try {
Class<?> c=Class.forName("com.app7.Sample");
@SuppressWarnings("deprecation")
Object ob=c.newInstance();
System.out.println(ob);
} catch (InstantiationException | IllegalAccessException |
ClassNotFoundException e) {
e.printStackTrace();
}
}

}
class Sample{}

2
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

3. Numeric with Underscore :-


In Java SE 7 and later, any number of underscore characters (_) can appear
anywhere between digits in a numerical literal. This feature enables you, for
example, to separate groups of digits in numeric literals, which can improve
the readability of your code.
For instance, if your code contains numbers with many digits, you can use an
underscore character to separate digits in groups of three, similar to how you
would use a punctuation mark like a comma, or a space, as a separator.

package com.app7;

public class NumericswithUnderscoreTest {

public static void main(String[] args) {


int sid=10_0; //_10 or 10_ invalid
System.out.println(sid);

float data=5.5_8f; //5.5_8_f invalid


System.out.println(data);

int aa=0b1100_10_10;
System.out.println(aa);

int hex=0xCAFE_BABE;
System.out.println(hex);

long aadhar=5454_9898_5632L; //8838_L invalid


System.out.println(aadhar);

double PI=3.14_15; //3_.14 invalid


System.out.println(PI);

}
}

3
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

4. Enhanced switch-case with String:-


In the JDK 7 release, you can use a String object in the expression of
a switch statement
The switch statement compares the String object in its expression with
the expressions associated with each case label as if it were using
the String.equals method; consequently, the comparison of String objects
in switch statements is case sensitive. The Java compiler generates generally
more efficient byte code from switch statements that use String objects than
from chained if-then-else statements.

package com.app7;

public class SwitchCaseTest {

public static void main(String[] args) {


String choice="good";
switch (choice) {
case "GREEN": System.out.println("Yours Green only!!");
break;
case "ORANGE": System.out.println("Yours Orange only!!");
break;
case "YELLOW": System.out.println("Yours Yellow only!!");
break;
case "RED": case "good": System.out.println("Yours RED GOOD
color!!"); break;

default:System.out.println("choosen nothing");
}
}
}

4
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

5. try-with-resource:-
The try-with-resources statement is a try statement that declares one or more
resources. A resource is as an object that must be closed after the program is
finished with it.
The try-with-resources statement ensures that each resource is closed at
the end of the statement. Any object that implements java.lang.AutoCloseable,
which includes all objects which implement java.io.Closeable, can be used as a
resource.

package com.app7;

import java.io.Closeable;
import java.io.IOException;

public class TryWithResource {

public static void main(String[] args) throws Exception {

System.out.println("in cls");
try (MySource m=new MySource();Another a=new
Another();Advanced a2=new Advanced()){
System.out.println("in try");
} catch (Exception e) {
System.out.println(e.getMessage());
}/*finally {
System.out.println("in finally");
}*/

}
}
class Another implements AutoCloseable{

@Override
public void close() throws Exception {
System.out.println("another close");
}

5
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

class MySource implements AutoCloseable{

@Override
public void close() throws Exception {
System.out.println("im closed");
}

}
class Advanced implements Closeable{

@Override
public void close() throws IOException {
System.out.println("advanced closeable");
}

6. Type Inference in Generics:-

You can replace the type arguments required to invoke the constructor
of a generic class with an empty set of type parameters (<>) as long as the
compiler can infer the type arguments from the context. This pair of angle
brackets is informally called the diamond.

package com.app7;

import java.util.ArrayList;
import java.util.List;

public class TypeInferenceInGenericsTest {

public static void main(String[] args) {


//in JDK 6 gives compile time error
List<String> mydata=new ArrayList<>();
mydata.add("A");
mydata.add("A");
System.out.println(mydata);

6
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD

Message<String> m=new Message<>();


m.show("Hello");

Message<Integer> m2=new Message<>();


m2.show(3+3);
}
}
class Message<T>{
void show(T a) {
System.out.println(a);
}
}

FB GROUP: https://www.facebook.com/groups/thejavatemple/

EMAIL : javabyraghu@gmail.com

You might also like