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.
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 ☺️
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.
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"; } }
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
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.
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
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
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 .
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?
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
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.
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); } }
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.?
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.
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 … 😊
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?
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.
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...
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..
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'.
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 ?
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
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
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?
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
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
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...
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.
@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.
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);;
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.
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 ☺️
Have seen many of your videos. This seems the most confusing one.
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.
i also felt the same, others were too simple to understand
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";
}
}
perfect
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
@20:03, you can also create instance in-place. Eg.
mp.print(str, new StringParser::convert);
won't it be new StringParser() ::convert ?
@@tanaykamath1415 no, because it's static
Yes Exactly@@tanaykamath1415
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.
thank you so
Out of all videos, this was the most difficult one to understand.
i was about to write the same
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
Sir ur the best..in the world bcs u explain concepts in simple language..
Only through your videos, my concepts get crystal clear. 👌👌
17:48 all you had to do was remove the opening parenthesis of the first 's'.
dude it happens when u got box vision.
That's a solid explanation Sir. Hats off to you.
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
Sir only because of you I got selected one of the top MNC
All C++ concepts are coming back in Java!
Those who worked with C++ will find this easy! :)
More of Javascript coming into Java
@@wmv1990 exactly
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 .
Thank you very much sir for all these wonderful videos... you are a guru 🙏
Thank ji providing valuable information about method reference.. 🙏🙏🙏
Learned alot Thankyou for this video
You are the man, beautiful explanations for all aliens.
Thanks a lot for the video. Compare to other videos this was little confusing...
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?
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
wow super features of java8. extremely code optimization .....and good explanation
Great video....guys please look for lambda videos first then it will be easy to understand
Helping With This Type Of Tutorials..Thank You..
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.
Thank you, brief and succinct....
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);
}
}
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);
}
}
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.?
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.
@@shoaibsilat Hi shoaib even I am confused on this we are implementng Parser Interface in class StringParser. How it is related
Very useful. Thanks!
awesome explanation. Keep it up bro
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 … 😊
Succinct. The interface Parser been provided with implementation to its only method parse(String) by passing the method reference.
Hey @Telusko, where can I get the code for this tutorial? There is no link related to it in the video's description. Thanks.
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?
Awesome 👍 can you please create a video about arbitrary objects
Wow! Thank you so much. Lambda Expressions && Method reference were very confusing subjects to me. But now I see it clearly! Thanks again!
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.
@17:48 you are not missing something, u have extra opening bracket which has to remove
Perfect as usual.
One ☝️ question out of subject - Why you greets viewers with Aliens 👽?
did you get that now?
//instance method reference to a object of specific class
Function toLowRef = String::toUpperCase;
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??
People hats Java because of this only...
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...
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..
for (StringParser :: convert) from where it's taking the param value?
str-->
Convert method has same arguments as parse method so it basically placing convert method as refrence to parse interface method internally.
Remember that the first parameter of mp.print() is str and MyPrinter uses that String as the data.
Hi,
So the method reference can be used only on functional interface which can be defined with single method call?
exactly
try adding 1 more method in interface it will give Functional Interface error
Using interface as method reference has optimization benefits, instead of calling direct static methods
which is optimized and should be preferred out of Lambda expression vs Method Reference?
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'.
Do we use this only to get simplified code?
it seems like like u know the concept, but unable to express it effectively.
not clear about the understanding of method reference.
Sir this video is awesome
please make a video on partial function and curry on java
Sir if you do live project on java then it will be very beneficial for engineering student.
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 ?
he is saying with an interface , method is not defined and you can make its implementation according to your need.
Very nice example
So confusing, doesn't require so many classes to demonstrate Method Reference. Please make a clear, crisp, and simple video like you were doing.
Not Clear. Bit Confusing
Does the method reference have to be a static method?
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
Among all the videos I have watched till now...This one is the most confusing....
Very helpful.. 😇
question? who is implementing Parser interface here?
Hi. He defines parse() method of Parser interface by using anonymous class
AWESOME EXPERIENCE
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
Ctrl + o
Sir can you please provide us lecture on Java.awt.image..
what if there are two+ more parameters are passed in convert method, how can we pass those parameters when using method reference.
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?
Nice tutorial
why do you remove return?hepl me plzz
This one is very confusing and boring...Sir can you please try to make it little bit easy.
Can someone explain the purpose for doing all of this extra work?
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
so complex example to understand simple method reference concept :/ just wasted my 17 mins, will have to watch another video now...
How to call interface default method ?
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
Thank you 🙏😊
Legend took wrong examples for explanation 😑
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...
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.
quite confusing video
sir i had complete your java begginer tutorial and after what can i do to become java developer
Hello sir,after seeing all ur videos can i go to interview for java developer as Fresher
IMO yes, but passing definitely not high confidence interval
Great
@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.
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);;
}
}
good video..
Its confusing sir..............
What Bro what are you doing to explain simple one
TQ
Samajh me nahi aaya apr acha laga..!!
👍👍👍
It confused me so much
This video not good. Compare to others
hello :)
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
its so confusing ..
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();
}
sir please reply sir
totally confused
the moment you say aliens, i lose all my interest
Worst example so far 😂.. so confusing
You have ruined the power of method reference using this crap example.
WTF was that..??
would you like to know the explanation? i can help you.