Method Reference in Java 8

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024
  • Check out our website: www.telusko.com
    Follow Telusko on Twitter: / navinreddy20
    Follow on Facebook:
    Telusko : / teluskolearnings
    Navin Reddy : / navintelusko
    Follow Navin Reddy on Instagram: / navinreddy20
    Subscribe to our other channel:
    Navin Reddy : / @navinreddy
    Telusko Hindi :
    / @teluskohindi

Комментарии • 137

  • @dnavas7719
    @dnavas7719 6 лет назад +26

    For all confused people:
    What's happening in this line `mp.print(str, (StringParser::convert));` is that you're basically saying, "this function is the implementation for the `parse` function in the interface". You are implementing the interface "on the fly" so to speak. It's just very concise.

  • @tushargurav4550
    @tushargurav4550 2 года назад +9

    Yes sir !
    We enjoyed lot ! For a non cs student your playlist of 186 videos help me lot to learn Java 😭
    Hope in future I will get a good opportunity to work in reputed company and being financially self independent 😀 that day I'm a Real Alien 😉
    Also once in life after placement I will meet you 😊🥲
    You are Best teacher for me ☺️

  • @priyankashaw1238
    @priyankashaw1238 4 года назад +26

    Out of all videos, this was the most difficult one to understand.

  • @mohsinr3
    @mohsinr3 7 лет назад +39

    Here is the Simple Example of Method Reference to understand.
    public class Java8MethodReference {
    public static void main(String[] args) {
    Parser p = StringConverter::convert;
    System.out.println(p.parse("Mohsin"));
    }
    }
    interface Parser{
    public String parse(String s);
    }
    class StringConverter{
    public static String convert(String s){
    return s+" is a Boss";
    }
    }

    • @sagardarade
      @sagardarade Год назад

      perfect

    • @Hhyxzz
      @Hhyxzz 11 месяцев назад

      Your code has some mistakes I think becoz u created parser interface and u created p object for that which is not possible and also in interface methods are public abstract by default but you mentioned public keyword in parser interface

  • @adarshtiwari9242
    @adarshtiwari9242 6 лет назад +13

    sir your all vedio provide me a very good knowledge about java 8 features . and especially your way of teaching of hard concept in simple way .
    thanks sir

  • @apdarshan
    @apdarshan 7 лет назад +35

    @20:03, you can also create instance in-place. Eg.
    mp.print(str, new StringParser::convert);

    • @tanaykamath1415
      @tanaykamath1415 4 года назад +3

      won't it be new StringParser() ::convert ?

    • @-karter-4556
      @-karter-4556 2 года назад +1

      @@tanaykamath1415 no, because it's static

    • @saju517
      @saju517 7 месяцев назад

      Yes Exactly@@tanaykamath1415

  • @prathmeshshirsat2134
    @prathmeshshirsat2134 4 года назад +2

    Hi sir, I have started learning Java 8 features from yesterday. Sir for learning java I prefer only your videos, your explanation on concepts is very simple and more understandable. This example of method reference was really awesome.

  • @shridhar_rao
    @shridhar_rao 5 лет назад +103

    Have seen many of your videos. This seems the most confusing one.

    • @-karter-4556
      @-karter-4556 2 года назад +2

      How is it confusing? It's a functional interface with only one method, therefore instead of creating an entirely different class, Java allows you to pass in your own implementation of that interface using lambda expression. Again this won't cause any issues because *there is only one method* .
      Anonymous inner classes can be used for multiple implementations because you specify the header of the method before you write the implementation.

    • @urrahman196
      @urrahman196 2 месяца назад

      i also felt the same, others were too simple to understand

  • @aniruddwakikar3004
    @aniruddwakikar3004 4 года назад +3

    Sir ur the best..in the world bcs u explain concepts in simple language..

  • @ganeshbodduaddasuryanitinb7811
    @ganeshbodduaddasuryanitinb7811 2 года назад +1

    I have one doubt. Parser is a interface. StringParser class is not implemented the Parser interface but you are passing StringParser object to print method. How it is working?

    • @JavaAll754
      @JavaAll754 Год назад

      It s static method inside stringparser.he created object for interface and defining anonymous code for abstract method and inside that method calling stringParser's convert method with class name

  • @tanulakhwani
    @tanulakhwani 11 месяцев назад

    Only through your videos, my concepts get crystal clear. 👌👌

  • @anb4351
    @anb4351 3 года назад +5

    This is only topic that I found a bit difficult to wrap my head around. I feel like doing it the lambda expression way is good enough, Its both concise and readable. Whereas Method Reference feels like making things extra concise but also making it hard to read at a glance on what is going on, it can be a bit confusing specially when method has parameters. I don't get the use case

  • @bharatmakkar9706
    @bharatmakkar9706 Год назад +1

    That's a solid explanation Sir. Hats off to you.

  • @brunosantanajava
    @brunosantanajava Год назад

    Hey @Telusko, where can I get the code for this tutorial? There is no link related to it in the video's description. Thanks.

  • @MahendraBabuRajendran
    @MahendraBabuRajendran 6 лет назад +11

    All C++ concepts are coming back in Java!
    Those who worked with C++ will find this easy! :)

    • @wmv1990
      @wmv1990 3 года назад +2

      More of Javascript coming into Java

    • @sagardarade
      @sagardarade Год назад

      @@wmv1990 exactly

  • @chakrapani_nallam
    @chakrapani_nallam 4 года назад +4

    Well, what's happening around 18:50 is that when the parse function of the Parser interface is called. it simply passes the method convert with argument str. This convert function returns "NAV", and that is printed .

  • @JoginderSingh-ko6fw
    @JoginderSingh-ko6fw 4 года назад

    Can anybody tell be should I watch this series in 2020 And do it cover all the concepts upto expert level or just core java. If it does not then please tell me whats remaining and what should I learn after this to be a advance java programmer who can do any possible thing in java(I know nobody can be a expert but i should be able to create any kinda app anything that we can do with java) please tell me..

  • @akashchoudhary9161
    @akashchoudhary9161 2 года назад +1

    Thank you very much sir for all these wonderful videos... you are a guru 🙏

  • @majesticmashup4657
    @majesticmashup4657 11 месяцев назад

    Can you please tell me where we have implemented that parse method of that parser Interface??
    What is the use of that Interface in the Example??

  • @nandagopal375
    @nandagopal375 Год назад

    Thank ji providing valuable information about method reference.. 🙏🙏🙏

  • @Gorky25
    @Gorky25 3 года назад

    Do we use this only to get simplified code?

  • @nicoqueijo
    @nicoqueijo 5 лет назад +13

    17:48 all you had to do was remove the opening parenthesis of the first 's'.

  • @pradeepn5658
    @pradeepn5658 2 года назад

    Sir only because of you I got selected one of the top MNC

  • @revathyrv2554
    @revathyrv2554 4 года назад +2

    Thanks a lot for the video. Compare to other videos this was little confusing...

  • @ganeshadepu6372
    @ganeshadepu6372 4 года назад +2

    wow super features of java8. extremely code optimization .....and good explanation

  • @sathyamoorthyp1212
    @sathyamoorthyp1212 4 года назад +1

    Hi,
    So the method reference can be used only on functional interface which can be defined with single method call?

    • @sagardarade
      @sagardarade Год назад

      exactly
      try adding 1 more method in interface it will give Functional Interface error

  • @hopearia6720
    @hopearia6720 2 года назад

    You are the man, beautiful explanations for all aliens.

  • @liveonphoenix5045
    @liveonphoenix5045 2 года назад

    Um, guys, for what I've comprehended; why does it so difficult to point out directly the most important part, when we need to pass a "method reference" as an argument or pass it as a R-Val, first the method parameter or the L-Val/the receiving end must be a "lambda" or a functional interface which has a single "public abstract method", the "return type" and "parameter(s)" of that abstract method must be the same with the given "method reference"..... Right?

  • @vktechie
    @vktechie 6 лет назад

    For instance method it is showing error in your example as you explained if convert is an instance method, we need to call it like new StringParser()::convert . I accept it, but how it work for "Collections.sort(list, Integer::compareTo);" here also compareTo is an instance method and still able to call "ClassName::InstanceMethod" . I am confusing with this...

  • @sagarmali5764
    @sagarmali5764 5 лет назад +1

    Great video....guys please look for lambda videos first then it will be easy to understand

  • @keshavagg
    @keshavagg 5 лет назад

    bit confusing about the usage.. because we could use simply mp.print( sp.convert(s) ) and change the print body accordingly. So what is the special use to pass method name.

  • @divyaswapnikachanna5182
    @divyaswapnikachanna5182 3 года назад

    Helping With This Type Of Tutorials..Thank You..

  • @seabass6106
    @seabass6106 3 года назад

    Wow! Thank you so much. Lambda Expressions && Method reference were very confusing subjects to me. But now I see it clearly! Thanks again!

  • @shreyanshjain2347
    @shreyanshjain2347 6 лет назад

    which is optimized and should be preferred out of Lambda expression vs Method Reference?

    • @lajos-berenyi
      @lajos-berenyi 4 года назад

      It is simple: if you use it only once, the lambda experession is preferrable. But if you use it many times, the method reference is preferrable. Or if you already have the method for another purpose, also more simple just to use it. For example the code 's -> s.toUpperCase' is identical with 'String::toUpperCase'.

  • @sivaramsomanchi5525
    @sivaramsomanchi5525 7 лет назад +5

    I didn't get one thing here. We have to pass Parser reference actually but you passed another class method there. How they are related to each other. I mean, how interface parser and class StringParser are related.?

    • @shoaibsilat
      @shoaibsilat 7 лет назад +4

      compiler know that it has to implement parser interface and in that interface there is a method to implement,it done this part automatically.you have to mention only what you want to do in the parse method.. so you simply pass an other method of class string parser.

    • @nagarjunmopuru7003
      @nagarjunmopuru7003 Год назад

      @@shoaibsilat Hi shoaib even I am confused on this we are implementng Parser Interface in class StringParser. How it is related

  • @midhunrajr372
    @midhunrajr372 4 года назад +2

    I think there is no simplest implementation than this one. List.foreach() only requires the method reference because the list object already contains the data. But in this case MyPrinter object is not holding any data so we are passing the String str.

  • @sampatel288
    @sampatel288 4 года назад +1

    Does the method reference have to be a static method?

    • @rovsenhuseynov8368
      @rovsenhuseynov8368 4 года назад +1

      Hi. Did not you watch the video carefully? The answer is in the video, but as you already asked, let me help you and answer your question.
      You call both static and non-static methods by using method reference.
      1. When you have a static method, you do not have to create an instance of the class, you can call the static method by using class name, so your method reference will be like className::methodName
      2. When you have a non-static method, firstly, you have to create an instance of the class, only after that you can call the method, so your method reference will be like instanceName::methodName

  • @hemanthselvam9645
    @hemanthselvam9645 3 года назад

    Awesome 👍 can you please create a video about arbitrary objects

  • @kakashihatake4840
    @kakashihatake4840 7 лет назад +4

    for (StringParser :: convert) from where it's taking the param value?

    • @jeevanreddy58
      @jeevanreddy58 7 лет назад +1

      str-->

    • @gauravvijan03
      @gauravvijan03 5 лет назад +1

      Convert method has same arguments as parse method so it basically placing convert method as refrence to parse interface method internally.

    • @midhunrajr372
      @midhunrajr372 4 года назад

      Remember that the first parameter of mp.print() is str and MyPrinter uses that String as the data.

  • @akshitapradeep7115
    @akshitapradeep7115 2 года назад +1

    Very useful. Thanks!

  • @pawebugiel319
    @pawebugiel319 Год назад

    awesome explanation. Keep it up bro

  • @HussainMohammedAshruf
    @HussainMohammedAshruf 3 года назад

    Succinct. The interface Parser been provided with implementation to its only method parse(String) by passing the method reference.

  • @9176648328
    @9176648328 4 года назад

    Very good content, clear and crisp explanation. Just one question, @11:53 video says "Whenever we have a class, The method's implementation is static. When there is an interface the method can vary". Could anyone kind enough to throw some more light on this ?

    • @shobhitsingh7735
      @shobhitsingh7735 3 года назад

      he is saying with an interface , method is not defined and you can make its implementation according to your need.

  • @crazeeealgorithms3236
    @crazeeealgorithms3236 4 года назад +3

    Thank you, brief and succinct....

  • @aishikbhattacharya4870
    @aishikbhattacharya4870 6 лет назад

    what is the eclipse short cut to see the methods of the inbuilt class and interfaces? I am doing Ctrl + clicking on the class name but I am not not getting the list of methods

  • @nasreensayyad7404
    @nasreensayyad7404 Год назад

    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Consumer;
    public class Java8MethodReference1a {
    public static void main(String[] args) {
    List list = Arrays.asList("A", "B", "C");
    // anonymous class
    list.forEach(new Consumer() {
    @Override
    public void accept(String x) {
    SimplePrinter.print(x);
    }
    });
    // lambda expression
    list.forEach(x -> SimplePrinter.print(x));
    // method reference
    list.forEach(SimplePrinter::print);
    }
    }
    class SimplePrinter {
    public static void print(String str) {
    System.out.println(str);
    }
    }
    Hi Navin sir… i am learnig alot from your videos … this is the only video i am little confused … so thought of sharing some clear code … 😊

  • @bytecodeskool
    @bytecodeskool 5 лет назад

    Sir if you do live project on java then it will be very beneficial for engineering student.

  • @rameshkumarguntamukkala8751
    @rameshkumarguntamukkala8751 4 года назад

    what if there are two+ more parameters are passed in convert method, how can we pass those parameters when using method reference.

    • @midhunrajr372
      @midhunrajr372 4 года назад +1

      We are passing the reference of the method and not its parameters. That is the most important thing to understand from this video. The method print() is the actual place where the parser() method is called. get it?

  • @amanpandey6427
    @amanpandey6427 6 лет назад

    Sir this video is awesome
    please make a video on partial function and curry on java

  • @priya021084
    @priya021084 Год назад +1

    not clear about the understanding of method reference.

  • @MYSELFSHUBHAM
    @MYSELFSHUBHAM 5 лет назад

    @17:48 you are not missing something, u have extra opening bracket which has to remove

  • @shredharc
    @shredharc 4 года назад +1

    question? who is implementing Parser interface here?

    • @rovsenhuseynov8368
      @rovsenhuseynov8368 4 года назад

      Hi. He defines parse() method of Parser interface by using anonymous class

  • @The_Ashwatthama
    @The_Ashwatthama 4 года назад

    why do you remove return?hepl me plzz

  • @Dp_writes
    @Dp_writes Год назад

    //instance method reference to a object of specific class
    Function toLowRef = String::toUpperCase;

  • @markwagner4273
    @markwagner4273 4 года назад

    Can someone explain the purpose for doing all of this extra work?

    • @midhunrajr372
      @midhunrajr372 4 года назад

      Example would be foreach() of List. We could make an external loop and process the list elements our-self, but when the List class itself implements its internal iterating solution, it would be more efficient.
      For easy understanding, lets imagine we have a class A. A has its data and methods to manipulate those data (obviously). Then if we want to add some more data manipulation functions, we (obviously) implement that inside class A. We could take the data from object of A from some external place and process those data from there (like looping through a list), but the first way (the foreach() way) is more efficient right?
      edit: and also we don't have to write a lot of boilerplate codes like: for (int i=0.... blah blah

  • @viramdesai3433
    @viramdesai3433 5 лет назад +2

    Perfect as usual.
    One ☝️ question out of subject - Why you greets viewers with Aliens 👽?

  • @satyamramawat
    @satyamramawat 4 года назад +1

    So confusing, doesn't require so many classes to demonstrate Method Reference. Please make a clear, crisp, and simple video like you were doing.

  • @gajeshtripathi5721
    @gajeshtripathi5721 3 года назад

    Very nice example

  • @somdeepsarkar2011
    @somdeepsarkar2011 5 лет назад +1

    Sir can you please provide us lecture on Java.awt.image..

  • @dipaliyadav-koli1060
    @dipaliyadav-koli1060 4 года назад

    Very helpful.. 😇

  • @pavanreddy9321
    @pavanreddy9321 7 лет назад

    Hello sir u r doing videos n helping us a lot but i have asked u so many times this question but i haven't got any reply from u sir :
    i have watched many of your videos, and really you are doing well, sir, please guide me i am confuse little bit, i started learning java before 3 month but still i can't able to think about how to build program, like i can't able to implements the logic sometime i feel i can't be a good program, please sir let me know how can i make my logic strong, and what the way to think about logic of the program while writing the code, help me i am waiting for your response...

    • @muhammadahmedalibaig5712
      @muhammadahmedalibaig5712 7 лет назад +3

      By solving more problems slowly slowly your logic will become strong. All you need is time and practice.
      1) Try to solve problem on page first.
      2) Do solve 1-2 small problems daily.
      3) Revisit your already solved problems.

  • @Ahmed-gf9bx
    @Ahmed-gf9bx 3 года назад

    Using interface as method reference has optimization benefits, instead of calling direct static methods

  • @OODevelopers
    @OODevelopers 6 лет назад +8

    it seems like like u know the concept, but unable to express it effectively.

  • @karandeepsingh1898
    @karandeepsingh1898 3 года назад

    AWESOME EXPERIENCE

  • @shivislifestyle9858
    @shivislifestyle9858 4 года назад +2

    Among all the videos I have watched till now...This one is the most confusing....

  • @kakashihatake4840
    @kakashihatake4840 7 лет назад +3

    Not Clear. Bit Confusing

  • @sagarrawat3246
    @sagarrawat3246 6 лет назад

    sir i had complete your java begginer tutorial and after what can i do to become java developer

  • @weekendhoppers
    @weekendhoppers 3 года назад

    Thank you 🙏😊

  • @dev.jacek.grzegorczyk
    @dev.jacek.grzegorczyk 3 года назад

    Nice tutorial

  • @krunalpatel9427
    @krunalpatel9427 6 лет назад +1

    This one is very confusing and boring...Sir can you please try to make it little bit easy.

  • @introverttoextrovert665
    @introverttoextrovert665 6 лет назад

    Hello sir,after seeing all ur videos can i go to interview for java developer as Fresher

  • @sintumehta9601
    @sintumehta9601 5 лет назад +2

    How to call interface default method ?

    • @jha.brajesh
      @jha.brajesh 5 лет назад +1

      Create a class and implement the interface containing default method, then call the default method by creating instance of it.
      U can use it for method reference too

  • @avinashshrivastava1512
    @avinashshrivastava1512 2 года назад

    People hats Java because of this only...

  • @Change_willcome
    @Change_willcome 2 года назад

    Great

  • @vikrantverma231
    @vikrantverma231 6 лет назад +1

    so complex example to understand simple method reference concept :/ just wasted my 17 mins, will have to watch another video now...

  • @nilupulheshan27
    @nilupulheshan27 2 года назад

    public class Demo {

    public static void myMethod(String str){
    System.out.println("*"+str);
    }
    public static void main(String[] args) {
    List names = Arrays.asList("Kamal","Nimal","Naveen","Priya");

    names.forEach(System.out::println);
    names.forEach(Demo::myMethod);

    }
    }

  • @bsrvn
    @bsrvn 3 года назад

    public class Mine {
    public static void main(String[] args) {
    List list = Arrays.asList(1,2,3,4,5,6);
    list.forEach(Mine::show);
    }
    private static void show(int i){
    System.out.println(i);
    }
    }

  • @m.arslankhan7449
    @m.arslankhan7449 5 лет назад

    @2:58 you said " object of Consumer interface"???? It's wrong, you can not make an object of the interface, 'con' is the object of the anonymous class.

  • @backtobasics6865
    @backtobasics6865 6 лет назад +3

    quite confusing video

  • @maheshmarpudi5648
    @maheshmarpudi5648 5 лет назад

    good video..

  • @SarojKumar-lb5gs
    @SarojKumar-lb5gs 7 лет назад +3

    Its confusing sir..............

  • @rawoof5576
    @rawoof5576 5 лет назад

    What Bro what are you doing to explain simple one

  • @start1learn-n171
    @start1learn-n171 3 года назад

    TQ

  • @SK-rx8yq
    @SK-rx8yq 2 года назад +1

    Legend took wrong examples for explanation 😑

  • @morolillyshean8816
    @morolillyshean8816 2 года назад

    👍👍👍

  • @saudsiddiqui5698
    @saudsiddiqui5698 3 года назад

    Samajh me nahi aaya apr acha laga..!!

  • @akshay070
    @akshay070 3 года назад

    This video not good. Compare to others

  • @aninditasarkar1164
    @aninditasarkar1164 Год назад

    It confused me so much

  • @sarath6789
    @sarath6789 5 лет назад +1

    This is the simple Code can use map for Conversion:
    package lambda.java.excersices;
    import java.util.Arrays;
    import java.util.List;
    class PrintLogic
    {
    public static void printNames(String s)
    {
    System.out.println(s);
    }
    }
    class ConvertImpl
    {
    public static String converImpMethod(String str)
    {
    if(str.startsWith("S"))
    str=str+" Super Hero ";
    else
    str=str+" loved by super heros";
    return str;
    }
    }
    public class MethodReferenceJava8 {
    public static void main(String[] args)
    {
    List heros = Arrays.asList("Sarath", "SPYDER","Son","IronMan");
    heros.stream().map(ConvertImpl::converImpMethod).forEach(PrintLogic::printNames);;

    }
    }

  • @sandeepkumarrayala6738
    @sandeepkumarrayala6738 2 года назад

    its so confusing ..

  • @abdulrashidsinancu1630
    @abdulrashidsinancu1630 9 месяцев назад

    totally confused

  • @jatinbansal4071
    @jatinbansal4071 5 лет назад

    You might have info or knowledge of the concept in great deal. but really, you people don't know how to make the concept clear to the watchers

  • @smanfly
    @smanfly 7 лет назад

    hello :)

  • @PrinceKumar-jn5zx
    @PrinceKumar-jn5zx 4 года назад

    You have ruined the power of method reference using this crap example.

  • @sanjay_ss
    @sanjay_ss Год назад

    Worst example so far 😂.. so confusing

  • @sagarrawat3246
    @sagarrawat3246 6 лет назад

    sir please reply sir

  • @rohit141996
    @rohit141996 9 месяцев назад

    the moment you say aliens, i lose all my interest

  • @golu4625
    @golu4625 Год назад

    Bakwasss

  • @paramita89
    @paramita89 6 лет назад

    public class Printer {
    public void printOnPrinter()
    {
    System.out.print("Printer : ");
    }
    public void printOnScreen()
    {
    System.out.print("Screen : ");
    }
    }
    public class PrintingTask {
    void printingTask(String message, Location loc) {
    loc.locate();
    System.out.println(message);
    }
    public static void main(String[] args) {
    Printer printer = new Printer();
    PrintingTask pt = new PrintingTask();
    pt.printingTask("Hello", printer::printOnPrinter);
    pt.printingTask("Hi", printer::printOnScreen);
    }
    }
    interface Location{
    void locate();
    }

  • @thetravelingraf
    @thetravelingraf 3 года назад

    17 min wasted