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











up vote
8
down vote

favorite
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















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 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 at 12:49















up vote
8
down vote

favorite
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















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 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 at 12:49













up vote
8
down vote

favorite
2









up vote
8
down vote

favorite
2






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 at 13:50









Eran

274k35438521




274k35438521










asked Nov 21 at 12:46









Clatty 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 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 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 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 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 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 at 12:49












2 Answers
2






active

oldest

votes

















up vote
16
down vote



accepted










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






























    up vote
    5
    down vote













    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 at 12:53








    • 3




      @glglgl Actually, the second type, right?
      – Ankur Chrungoo
      Nov 21 at 12:57












    • With 0-based counting even the 1st :P
      – Max Vollmer
      Nov 21 at 13:00




















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    16
    down vote



    accepted










    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



























      up vote
      16
      down vote



      accepted










      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

























        up vote
        16
        down vote



        accepted







        up vote
        16
        down vote



        accepted






        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 at 12:56

























        answered Nov 21 at 12:51









        Eran

        274k35438521




        274k35438521
























            up vote
            5
            down vote













            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 at 12:53








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              Nov 21 at 12:57












            • With 0-based counting even the 1st :P
              – Max Vollmer
              Nov 21 at 13:00

















            up vote
            5
            down vote













            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 at 12:53








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              Nov 21 at 12:57












            • With 0-based counting even the 1st :P
              – Max Vollmer
              Nov 21 at 13:00















            up vote
            5
            down vote










            up vote
            5
            down vote









            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 at 13:11

























            answered Nov 21 at 12:51









            Ankur Chrungoo

            43116




            43116








            • 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 at 12:53








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              Nov 21 at 12:57












            • With 0-based counting even the 1st :P
              – Max Vollmer
              Nov 21 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 at 12:53








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              Nov 21 at 12:57












            • With 0-based counting even the 1st :P
              – Max Vollmer
              Nov 21 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 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 at 12:53






            3




            3




            @glglgl Actually, the second type, right?
            – Ankur Chrungoo
            Nov 21 at 12:57






            @glglgl Actually, the second type, right?
            – Ankur Chrungoo
            Nov 21 at 12:57














            With 0-based counting even the 1st :P
            – Max Vollmer
            Nov 21 at 13:00






            With 0-based counting even the 1st :P
            – Max Vollmer
            Nov 21 at 13:00





            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар