Professional Documents
Culture Documents
Java (JDK) 7 New Features: 1. Binary Literals
Java (JDK) 7 New Features: 1. Binary Literals
Java (JDK) 7 New Features: 1. Binary Literals
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;
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
package com.app7;
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);
}
}
class Sample{}
2
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD
package com.app7;
int aa=0b1100_10_10;
System.out.println(aa);
int hex=0xCAFE_BABE;
System.out.println(hex);
}
}
3
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD
package com.app7;
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;
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
@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");
}
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;
6
-by RAGHU SIR SATHYA TECHNOLOGIES, AMEERPET, HYD
FB GROUP: https://www.facebook.com/groups/thejavatemple/
EMAIL : javabyraghu@gmail.com