00:00
xother1.png
/* Main1.java : create seperate class and impletement the funcional interface, old */ /* how to use functional interface mt*/ /* Way-1 : old way-before using functional interface */ package lambda; public class Main1 { public static void main(String[] args) { System.out.println("\n_ _My System starts........\n"); MyInter1 myInter = new MyInterImpl1(); //inherit the property on myInter MyInterImpl1 myInter1 = new MyInterImpl1(); myInter.sayHello(); myInter1.sayHello(); } }
/* MyInter1.java : functional interface */ package lambda; @FunctionalInterface public interface MyInter1 { public abstract void sayHello(); }
/* MyInterImpl1.java : separate class ( MyInterImp.java ) and implements this interface*/ package lambda; public class MyInterImpl1 implements MyInter1{ @Override public void sayHello() { System.out.println("_ _ I am saying hello from MyInterImp\n"); } }
/* Main2.java */ /* Way-2 : anonumus class */ package lambda; public class Main2 { public static void main(String[] args) { System.out.println("\n_ _My System starts........\n"); // not the parent but the object is for the anonymus child class */ MyInter1 mi1 = new MyInter1(){ @Override public void sayHello() { System.out.println("\n_ _by using anonymus class ........\n"); }}; mi1.sayHello(); MyInter1 mi2 = new MyInter1() { @Override public void sayHello() { System.out.println("\n_ _by using anonymus class second ........\n"); }}; mi2.sayHello(); } }
/* MyInter1.java : functional interface */ package lambda; @FunctionalInterface public interface MyInter1 { public abstract void sayHello(); }
/* Main3.java */ /* Way-3 : lambda expression */ /* Step 1. fnctional itf = 2. itf obj. with lambda + mt implementation 3. use object to override interface m/t copy whole the code, then delete everything , 1. even the name , 2. return type and 3. type in agrument */ public class Main3 { public static void main(String[] args) { System.out.println("\n_ _My System starts........\n"); /* public abstract void sayHello(); delete all except the () and implemet its method using {};*/ MyInter1 i = ()->System.out.println("by using LAMBDA EXPRESSIONS...\n"); i.sayHello(); Sumtwo st = (int a , int b) -> (a + b); // or /* Sumtwo st = (a , b) -> a+b;*/ System.out.println(st.sum(10, 20)); /* LenStr ls = (String str) -> str.length(); */ LenStr ls = (str) -> str.length(); System.out.println(ls.strLen("rajat")); } }
/* MyInter1.java : fun. itf. */ package lambda; @FunctionalInterface public interface MyInter1 { public abstract void sayHello(); // delete all, excepts () }
/* Sumtwo.java : fun. itf. */ package lambda; @FunctionalInterface public interface Sumtwo { int sum(int a, int b); }
/* LenStr.java : fun. itf. */ package lambda; @FunctionalInterface public interface LenStr { public abstract int strLen(String str); }
/* runnable is fun. itf , use it by lambda expression */ /* 1 to 10 , one sec delay */ package lambda; public class ThreadLambda { public static void main(String[] args) { Runnable rt = () -> { for(int i =1; i<=10;i++){ System.out.println("value of i is = "+i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; Thread t = new Thread(rt); t.setName("maoj"); t.start();
/* print tabel of 2 using the lambda expression */ Runnable rt1 = () -> { for(int i =1; i<=10;i++){ System.out.println("2 * "+i+"="+ i*2); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; Thread t1 = new Thread(rt1); t1.setName("rajat"); t1.start(); } }
/* DesktopApp.java : fun. itf. */ package lambda; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import javax.swing.*; public class DesktopApp{ public static void main(String[] args) { JFrame frame = new JFrame("My Window"); frame.setSize(400,400); frame.setLayout(new FlowLayout()); JButton button = new JButton("Click Me !!"); frame.add(button); /* button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(button, e, "this is click", 0); } }); */ //using fun. itf. button.addActionListener((ActionEvent e) -> { System.out.println("button click"); JOptionPane.showMessageDialog(null, "hey", "my message", 0); }); frame.add(button); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
xother2.png
WR01 - wrapper classs /* Wrapper Classes: Primitive API Class package parent Data Types (java.lang) boolean Boolean > java.lang > java.land.Object char Character > java.lang > java.land.Object byte Byte > java.lang > java.land.Number > java.land.Object short Short > java.lang > java.land.Number > java.land.Object int Integer > java.lang > java.land.Number > java.land.Object long Long > java.lang > java.land.Number > java.land.Object float Float > java.lang > java.land.Number > java.land.Object double Double > java.lang > java.land.Number > java.land.Object > all class are final > object of Wrapper class immutable > wrapper class me bahut sare constructor aur method hote hai > agar wrapper class na ho to programmer ko method bhee code karne hoge > collection me value object ke form me save hoti thee pahle , is leeaye iska use karte they */
public class WR02 { public static void main(String[] args){ /* use to make object of primitive type , wrapper cls ct old way , DEPRECIATED */ Integer i1 = new Integer(12); // 12 Integer i2 = new Integer("56"); // 56 Integer i3 = new Integer("a0"); // numberformatexception /* valueOf - new way wrapper class mt, PRIMITIVE TO OBJECT */ Integer i4 = Integer.valueOf(23); // 23 Character c1 = Character.valueOf('a'); // a /* valueOf : Boolean */ Boolean b1 = Boolean.valueOf(true); // true Boolean b2 = Boolean.valueOf(tRu); // false Boolean b3 = Boolean.valueOf(tRuE); // true Boolean b4 = Boolean.valueOf(raja); // false /* valueOf : Float */ Float f1 = Float.valueOf(1.2f); // 1.2 Float f2 = Float.valueOf(12.34); // lossy conversion from double to float Float f4 = Float.valueOf(12.34F); // F /* valueOf : int = hex , oct, binary */ Integer i5 = Integer.valueOf("111", 2); // Decimal = 7 Integer i6 = Integer.valueOf("111", 10); // Binary = 111 Integer i7 = Integer.valueOf("71", 8); // Octail = 57 Integer i8 = Integer.valueOf("a1", 16); // Hex = 57 System.out.println(i7); } }
/* int to byte within range, No Need to type cast */ public class WR03 { public static void main(String[] args) { byte x = 12; // works fine , range = -128 to 127 pro(x); } static void pro(byte x){ System.out.println(x); // 12 } }
/* int-byte out of range, compile time error */ public class WR04 { public static void main(String[] args) { byte x = 128; // cant convert form int to byte pro(x); } static void pro(byte x){ System.out.println(x); } }
/* direct mt call with int value, incompatible types: possible lossy conversion from int to byte */ public class WR05 { public static void main(String[] args) { byte b1 = 12; pro(12); // default int } static void pro(byte x){ System.out.println(x); } }
/* above with casting, direct mt call with int value with type cast */ public class WR06 { public static void main(String[] args) { byte b1 = 12; pro((byte)12); } static void pro(byte x){ System.out.println(x); // 12 } }
/* at time of primitive to object convrsion , need to specified type in brakcket */ public class WR07 { public static void main(String[] args) { Byte b1 = Byte.valueOf(12); // error : default int Byte b2 = Byte.valueOf((byte)12); // workks fine /* same with short */ Short b3 = Short.valueOf(34); // error : default int : no suitable mt found Short b4 = Short.valueOf((short)34); // 34 /* double to float object */ Float f1 = Float.valueOf(2.34); // error : double to float can not be converted Float f2 = Float.valueOf((float)2.34); Float f3 = Float.valueOf(2.34f); Float f4 = Float.valueOf(2.34F); /* Byte */ byte b1 = 128; // error : out of range .incompatible types: possible lossy conversion from int to byte byte b1 = 127; // in range Byte b2 = Byte.valueOf(127); // not applicable for the argument int Byte b3 = Byte.valueOf((byte)127); // type cast , works fine Byte b4 = Byte.valueOf(128); // not applicabel and out of range Byte b5 = Byte.valueOf((byte)128); // -128 Byte b6 = Byte.valueOf("56"); // 56 Byte b7 = Byte.valueOf("128"); // java.lang.NumberFormatException Byte b8 = Byte.valueOf("1a8"); // java.lang.NumberFormatException Short b9 = Short.valueOf(32767); // java.lang.NumberFormatException Short b10 = Short.valueOf((short)32767); // 32767 Short b11 = Short.valueOf((short)32768); // -32768 } }
Home PREVIOUS NEXT