JDK1.5
1. Generics - Avoid type casting
List<String> l = new ArrayList<String>();
2. Autoboxing(automatic conversion of datatype to wrapper class) and Unboxing(automatic conversion of wrapper class to datatype)
int a=10; //declaration of variable
Integer b; //declaration of class
b=a; //autoboxing //b=10 //b=new Integer(a);
int c=b; //unboxing //c=b.intValue();
3. Var args - variable number of arguments, denoted ...
void add(int a,int b) {
}
void add(int a,int b,int c) {
}
void add(int a,int b,int c,int d) {
}
void add(int...a) { //take 0 or n number of int args
}
void add(String...s) { //take 0 or n number of String args
}
void add(String...s,boolean b) { //error
}
void add(boolean b, double d,String...s) { //var args should be always last argument
}
add(false,3.4,"hello","world","hi");
add(true,1.4,"hello")
void add(boolean b, double...d,String...s) { //error - a method can take only 1 var args
}
4. static import
System.out.println()
System is a predefined class
public class System extends Object {
static PrintStream out;
}
We call static method and variable using classname.methodname or classname.variable, but if we want to invoke static method and variable without using classname.methodname or classname.variable, in that case we use static import
import static java.lang.System.out;
import static java.lang.Math.sqrt;
public class Main {
public static void main(String[] args) {
double d=sqrt(16); //static method
out.println(d);
}
}
5. for each stmt - enhanced for loop
double a[] = {1.1,2.2,3.3}
for(int i=0;i<a.length;i++) {
System.out.println(a[i])
}
1. you have to declare loop counter variable only inside for each loop and it should be of same datatype as an array
2. 1D will be stored in a variable, 2D will be stored in 1D and 3D will be stored in 2D etc
for(double d1:a)
System.out.println(d1);
int a1[][]={{1,2},{3,4}};
for(int a2[]:a1)
for(int a3:a2)
System.out.println(a3);
6. Assertions
7. Annotations
JDK1.7
1. Generics
List<String> l = new ArrayList<String>(); //JDK1.5
List<String> l = new ArrayList<>(); //JDK1.7
2. try with resources - to automatically close the resources which is opened
try(FileWriter fw=new FileWriter("a.txt");) {
}catch(Exception e){
}
3. Multi catch statement with | symbol
try{
}
catch(NullPointerException | ArthimeticException | ArrayIndexOutOfBoundsException e){
}
4. Binary literal
int a=0b100;
SOP(a); //4
int a1=0B11;
SOP(a1); //3
5. Underscore literal - only for representation
int a=100000;
int a=1_00_000;
int a1=10_00_000;
int b=10_; //error
float f1=3_.14f; //error
float f1=3._14f; //error
float f1=3.14_f; //error
float f1=3.1_4f; //correct
6. In Switch case, we can use String as an expr
JDK1.8 Features
1. Lambda expression
- used to enable functional programming in Java which contains functions that exists on its own and execute it independently
public class Main {
public void greet() {
System.out.println("Helloworld");
}
public static void main(String[] args) {
Main m=new Main();
Main m1=new Main();
Main m2=new Main();
m.greet();
m1.greet();
m2.greet();
}
}
Now greet() always prints only "Helloworld" for any number of objects, but we need greet() to print different information, until JDK1.7 if we need different implementations for the methods then we have to go for interface
interface Greeting {
void perform();
}
class Helloworld implements Greeting {
@Override
public void perform() {
System.out.println("Hello");
}
}
class Welcome implements Greeting {
@Override
public void perform() {
System.out.println("Welcome");
}
}
public class Main {
/*public void greet() {
System.out.println("Helloworld");
}*/
public void greet(Greeting g) {
g.perform();
}
public static void main(String[] args) {
Main m=new Main();
Greeting gr=new Helloworld(); //Dynamic Method Dispatch
m.greet(gr); //Hello
gr=new Welcome();
m.greet(gr); //Welcome
}
}
But we want to pass the behaviour directly (ie) perform() as an argument to greet() and execute the perform() independently, rather than passing the thing that contains perform() method (ie) Greeting interface
Lambda expression are just function that exists on its own and execute it independently, so that we are enabling functional programming in Java
How we write Lambda expression?
1. We take a function and assign to a variable
a = public void perform() {
System.out.println("Hello");
}
public, private and protected make sense if we write any method inside class, but in lambda expr they are just functions that exists on its own. So when we write any lambda expr there is no need to define access specifier
2. a = void perform() {
System.out.println("Hello");
}
Whenever we assign a function to a variable, then we can invoke the function by calling the variablename. So when we write any lambda expr there is no need to define function name
3. a = void () {
System.out.println("Hello");
return "10";
}
By seeing the function itself we can identiy the return of the function. So when we write any lambda expr there is no need to define return type
4. a = () {
System.out.println("Hello");
}
Lambda expr contains parenthesis for input argument, logic inside curly braces and use -> symbol
a = () -> {
System.out.println("Hello");
}
b = (int a,int b) -> {
return a+b;
}
5. FunctionType<Void,Void> a = () -> {
System.out.println("Hello");
}
FunctionalInterface a = () -> {
System.out.println("Hello");
}
- Using FunctionalInterface as a return type of lambda expr, this is called as Type Inference
- Lambda expr is always used to write logic only for the methods in Functional interface
Rules
1. If ur body of lambda expr is just a single line, then we can ignore curly braces
FunctionalInterface a = () -> System.out.println("Hello");
2. If ur body of lambda expr is just a single line, then we can ignore return stmt
FunctionalInterface b = (int a,int b) -> a+b;
Before JDK1.8, How we can access the methods in an interface - 2 ways
1. By implementing interface in a class
interface Greeting {
void perform();
}
class Helloworld implements Greeting {
@Override
public void perform() {
System.out.println("Hello");
}
}
public class Main {
public static void main(String[] args) {
Greeting gr=new Helloworld(); //Dynamic Method Dispatch
gr.perform(); //Hello
}
}
2. Use Anonmyous Inner class
interface Greeting {
void perform();
}
public class Main {
public static void main(String[] args) {
Greeting gr=new Greeting() { //Anonmyous Inner class
@Override
public void perform() {
System.out.println("Hello");
}
};
gr.perform();
}
}
FunctionalInterface
- contains only one abstract and any number of default and static methods
- We will write logic for functional interface method using lambda expr
- It is also called as SAM(Single Abstract Method)
- Using @FunctionalInterface annotation present in java.lang.* pkg - optional
- If a interface contains only method of Object class as abstract, then it is also considered as Functional Interface
//Functional Interface
interface A {
void add();
}
//Not Functional Interface
interface A {
void add();
void show();
}
//Functional Interface
interface A {
void add();
default void show() {
}
static void show1() {
}
}
//Functional Interface
interface A {
void add();
String toString(); //Object class
}
//Not Functional Interface
interface A {
void add();
boolean equals();
}
//Functional Interface
interface A {
void add();
boolean equals(Object o); //Object class
}
//Functional Interface
@FunctionalInterface
interface A {
void add();
}
//Functional Interface
interface A {
void add();
}
interface B extends A,C { //Not functional interface
void add1();
}
interface Greeting {
void perform();
}
public class Main {
public static void main(String[] args) {
Greeting g=()->System.out.println("Using Lambda");
g.perform(); //Using Lambda
}
}
//Functional Interface
interface MyInterface {
int calculateLength(String s);
}
public class Main {
public static void main(String[] args) {
MyInterface m=(e1)->e1.length();
System.out.println(m.calculateLength("hello")); //5
}
}
//Functional Interface
interface MyInterface {
void add(int a,int b);
}
public class Main {
public static void main(String[] args) {
MyInterface m1=(x,y)->System.out.println(x+y);
m1.add(10,20); //30
}
}
public class Main {
public static void main(String[] args) {
Thread t1=new Thread(new Runnable() {
public void run() {
System.out.println("Using Anonmyous class");
}
});
t1.run();
Thread t2=new Thread(()->System.out.println("Using Lambda"));
t2.run();
}
}
Handson
1. Write a lambda expression which accepts x and y numbers and return xy .
2. Write a method that uses lambda expression to format a given string, where a space is inserted between each character of string. For ex., if input is “CG”, then expected output is “C G”.
No comments:
Post a Comment