“new” Keyword In Java Lambda Method Reference [duplicate]












8
















This question already has an answer here:




  • Reference to an instance method of a particular object

    6 answers




I've seen a lot of methods where a new class is instantiated in a lambda method reference but can't seem to understand why. When is the new keyword needed in a method reference?



For example, the following passes compilation:



UnaryOperator<String>stringToUpperCase = String::toUpperCase;


But this doesn't:



UnaryOperator<String>stringToUpperCase = new String()::toUpperCase; 









share|improve this question















marked as duplicate by Federico Peralta Schaffner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 16:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4





    a new String in upper case is still just a blank string, so s -> "" will do the same thing

    – Michael
    Nov 21 '18 at 12:49
















8
















This question already has an answer here:




  • Reference to an instance method of a particular object

    6 answers




I've seen a lot of methods where a new class is instantiated in a lambda method reference but can't seem to understand why. When is the new keyword needed in a method reference?



For example, the following passes compilation:



UnaryOperator<String>stringToUpperCase = String::toUpperCase;


But this doesn't:



UnaryOperator<String>stringToUpperCase = new String()::toUpperCase; 









share|improve this question















marked as duplicate by Federico Peralta Schaffner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 16:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4





    a new String in upper case is still just a blank string, so s -> "" will do the same thing

    – Michael
    Nov 21 '18 at 12:49














8












8








8


2







This question already has an answer here:




  • Reference to an instance method of a particular object

    6 answers




I've seen a lot of methods where a new class is instantiated in a lambda method reference but can't seem to understand why. When is the new keyword needed in a method reference?



For example, the following passes compilation:



UnaryOperator<String>stringToUpperCase = String::toUpperCase;


But this doesn't:



UnaryOperator<String>stringToUpperCase = new String()::toUpperCase; 









share|improve this question

















This question already has an answer here:




  • Reference to an instance method of a particular object

    6 answers




I've seen a lot of methods where a new class is instantiated in a lambda method reference but can't seem to understand why. When is the new keyword needed in a method reference?



For example, the following passes compilation:



UnaryOperator<String>stringToUpperCase = String::toUpperCase;


But this doesn't:



UnaryOperator<String>stringToUpperCase = new String()::toUpperCase; 




This question already has an answer here:




  • Reference to an instance method of a particular object

    6 answers








java lambda java-8 method-reference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:50









Eran

281k37455541




281k37455541










asked Nov 21 '18 at 12:46









Clatty CakeClatty Cake

3293511




3293511




marked as duplicate by Federico Peralta Schaffner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 16:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Federico Peralta Schaffner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 16:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4





    a new String in upper case is still just a blank string, so s -> "" will do the same thing

    – Michael
    Nov 21 '18 at 12:49














  • 4





    a new String in upper case is still just a blank string, so s -> "" will do the same thing

    – Michael
    Nov 21 '18 at 12:49








4




4





a new String in upper case is still just a blank string, so s -> "" will do the same thing

– Michael
Nov 21 '18 at 12:49





a new String in upper case is still just a blank string, so s -> "" will do the same thing

– Michael
Nov 21 '18 at 12:49












2 Answers
2






active

oldest

votes


















16














String::toUpperCase is a method reference that can be applied to any String instance.



new String()::toUpperCase is a method reference that can be applied to a specific String instance (the instance created by new String()).



Since UnaryOperator<String> expects a method that takes a String and returns a String, String::toUpperCase fits (since you can apply it on a String and get the upper case version of that String).



On the other hand, new String()::toUpperCase doesn't fit UnaryOperator<String>, since it is executed on an already specified String, so you can't pass another String instance to it.



It can, however, by assigned to a Supplier<String>, since it simply supplies an empty String instance:



Supplier<String> emptyStringToUpperCase = new String()::toUpperCase; 


This is similar to:



Supplier<String> emptyStringToUpperCase = () -> new String().toUpperCase();


while this:



UnaryOperator<String> stringToUpperCase = String::toUpperCase;


is similar to:



UnaryOperator<String> stringToUpperCase = s -> s.toUpperCase();





share|improve this answer

































    5














    There are four kinds of method references as shown below and your type falls in the second category, but UnaryOperator<String> essentially needs to represent a method which accepts any String argument and returns a String. However, the non-working method reference that you have used is actually working on a particular String object (i.e. not any String object)



    enter image description here



    Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html






    share|improve this answer





















    • 1





      Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

      – glglgl
      Nov 21 '18 at 12:53








    • 3





      @glglgl Actually, the second type, right?

      – Ankur Chrungoo
      Nov 21 '18 at 12:57













    • With 0-based counting even the 1st :P

      – Max Vollmer
      Nov 21 '18 at 13:00




















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    16














    String::toUpperCase is a method reference that can be applied to any String instance.



    new String()::toUpperCase is a method reference that can be applied to a specific String instance (the instance created by new String()).



    Since UnaryOperator<String> expects a method that takes a String and returns a String, String::toUpperCase fits (since you can apply it on a String and get the upper case version of that String).



    On the other hand, new String()::toUpperCase doesn't fit UnaryOperator<String>, since it is executed on an already specified String, so you can't pass another String instance to it.



    It can, however, by assigned to a Supplier<String>, since it simply supplies an empty String instance:



    Supplier<String> emptyStringToUpperCase = new String()::toUpperCase; 


    This is similar to:



    Supplier<String> emptyStringToUpperCase = () -> new String().toUpperCase();


    while this:



    UnaryOperator<String> stringToUpperCase = String::toUpperCase;


    is similar to:



    UnaryOperator<String> stringToUpperCase = s -> s.toUpperCase();





    share|improve this answer






























      16














      String::toUpperCase is a method reference that can be applied to any String instance.



      new String()::toUpperCase is a method reference that can be applied to a specific String instance (the instance created by new String()).



      Since UnaryOperator<String> expects a method that takes a String and returns a String, String::toUpperCase fits (since you can apply it on a String and get the upper case version of that String).



      On the other hand, new String()::toUpperCase doesn't fit UnaryOperator<String>, since it is executed on an already specified String, so you can't pass another String instance to it.



      It can, however, by assigned to a Supplier<String>, since it simply supplies an empty String instance:



      Supplier<String> emptyStringToUpperCase = new String()::toUpperCase; 


      This is similar to:



      Supplier<String> emptyStringToUpperCase = () -> new String().toUpperCase();


      while this:



      UnaryOperator<String> stringToUpperCase = String::toUpperCase;


      is similar to:



      UnaryOperator<String> stringToUpperCase = s -> s.toUpperCase();





      share|improve this answer




























        16












        16








        16







        String::toUpperCase is a method reference that can be applied to any String instance.



        new String()::toUpperCase is a method reference that can be applied to a specific String instance (the instance created by new String()).



        Since UnaryOperator<String> expects a method that takes a String and returns a String, String::toUpperCase fits (since you can apply it on a String and get the upper case version of that String).



        On the other hand, new String()::toUpperCase doesn't fit UnaryOperator<String>, since it is executed on an already specified String, so you can't pass another String instance to it.



        It can, however, by assigned to a Supplier<String>, since it simply supplies an empty String instance:



        Supplier<String> emptyStringToUpperCase = new String()::toUpperCase; 


        This is similar to:



        Supplier<String> emptyStringToUpperCase = () -> new String().toUpperCase();


        while this:



        UnaryOperator<String> stringToUpperCase = String::toUpperCase;


        is similar to:



        UnaryOperator<String> stringToUpperCase = s -> s.toUpperCase();





        share|improve this answer















        String::toUpperCase is a method reference that can be applied to any String instance.



        new String()::toUpperCase is a method reference that can be applied to a specific String instance (the instance created by new String()).



        Since UnaryOperator<String> expects a method that takes a String and returns a String, String::toUpperCase fits (since you can apply it on a String and get the upper case version of that String).



        On the other hand, new String()::toUpperCase doesn't fit UnaryOperator<String>, since it is executed on an already specified String, so you can't pass another String instance to it.



        It can, however, by assigned to a Supplier<String>, since it simply supplies an empty String instance:



        Supplier<String> emptyStringToUpperCase = new String()::toUpperCase; 


        This is similar to:



        Supplier<String> emptyStringToUpperCase = () -> new String().toUpperCase();


        while this:



        UnaryOperator<String> stringToUpperCase = String::toUpperCase;


        is similar to:



        UnaryOperator<String> stringToUpperCase = s -> s.toUpperCase();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 12:56

























        answered Nov 21 '18 at 12:51









        EranEran

        281k37455541




        281k37455541

























            5














            There are four kinds of method references as shown below and your type falls in the second category, but UnaryOperator<String> essentially needs to represent a method which accepts any String argument and returns a String. However, the non-working method reference that you have used is actually working on a particular String object (i.e. not any String object)



            enter image description here



            Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html






            share|improve this answer





















            • 1





              Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

              – glglgl
              Nov 21 '18 at 12:53








            • 3





              @glglgl Actually, the second type, right?

              – Ankur Chrungoo
              Nov 21 '18 at 12:57













            • With 0-based counting even the 1st :P

              – Max Vollmer
              Nov 21 '18 at 13:00


















            5














            There are four kinds of method references as shown below and your type falls in the second category, but UnaryOperator<String> essentially needs to represent a method which accepts any String argument and returns a String. However, the non-working method reference that you have used is actually working on a particular String object (i.e. not any String object)



            enter image description here



            Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html






            share|improve this answer





















            • 1





              Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

              – glglgl
              Nov 21 '18 at 12:53








            • 3





              @glglgl Actually, the second type, right?

              – Ankur Chrungoo
              Nov 21 '18 at 12:57













            • With 0-based counting even the 1st :P

              – Max Vollmer
              Nov 21 '18 at 13:00
















            5












            5








            5







            There are four kinds of method references as shown below and your type falls in the second category, but UnaryOperator<String> essentially needs to represent a method which accepts any String argument and returns a String. However, the non-working method reference that you have used is actually working on a particular String object (i.e. not any String object)



            enter image description here



            Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html






            share|improve this answer















            There are four kinds of method references as shown below and your type falls in the second category, but UnaryOperator<String> essentially needs to represent a method which accepts any String argument and returns a String. However, the non-working method reference that you have used is actually working on a particular String object (i.e. not any String object)



            enter image description here



            Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 13:11

























            answered Nov 21 '18 at 12:51









            Ankur ChrungooAnkur Chrungoo

            62039




            62039








            • 1





              Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

              – glglgl
              Nov 21 '18 at 12:53








            • 3





              @glglgl Actually, the second type, right?

              – Ankur Chrungoo
              Nov 21 '18 at 12:57













            • With 0-based counting even the 1st :P

              – Max Vollmer
              Nov 21 '18 at 13:00
















            • 1





              Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

              – glglgl
              Nov 21 '18 at 12:53








            • 3





              @glglgl Actually, the second type, right?

              – Ankur Chrungoo
              Nov 21 '18 at 12:57













            • With 0-based counting even the 1st :P

              – Max Vollmer
              Nov 21 '18 at 13:00










            1




            1





            Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

            – glglgl
            Nov 21 '18 at 12:53







            Strictly speaking, new String()::toUpperCase is indeed a method reference, of the third kind (new String() is an object which has the toUpperCase method). It doesn't take arguments, but returns a String. It could be used as a Supplier<String>. But it is a very complicated way to say () -> "".

            – glglgl
            Nov 21 '18 at 12:53






            3




            3





            @glglgl Actually, the second type, right?

            – Ankur Chrungoo
            Nov 21 '18 at 12:57







            @glglgl Actually, the second type, right?

            – Ankur Chrungoo
            Nov 21 '18 at 12:57















            With 0-based counting even the 1st :P

            – Max Vollmer
            Nov 21 '18 at 13:00







            With 0-based counting even the 1st :P

            – Max Vollmer
            Nov 21 '18 at 13:00





            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin