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











up vote
7
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();
}
);
});
});
12 hours ago


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
    15 hours ago















up vote
7
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();
}
);
});
});
12 hours ago


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
    15 hours ago













up vote
7
down vote

favorite
2









up vote
7
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 14 hours ago









Eran

273k35432516




273k35432516










asked 15 hours ago









Clatty Cake

3193511




3193511




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();
}
);
});
});
12 hours ago


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();
}
);
});
});
12 hours ago


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
    15 hours ago














  • 4




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








4




4




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




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












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
      15 hours ago








    • 3




      @glglgl Actually, the second type, right?
      – Ankur Chrungoo
      15 hours ago












    • With 0-based counting even the 1st :P
      – Max Vollmer
      15 hours ago




















    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 15 hours ago

























        answered 15 hours ago









        Eran

        273k35432516




        273k35432516
























            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
              15 hours ago








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              15 hours ago












            • With 0-based counting even the 1st :P
              – Max Vollmer
              15 hours ago

















            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
              15 hours ago








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              15 hours ago












            • With 0-based counting even the 1st :P
              – Max Vollmer
              15 hours ago















            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 14 hours ago

























            answered 15 hours ago









            Ankur Chrungoo

            41116




            41116








            • 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
              15 hours ago








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              15 hours ago












            • With 0-based counting even the 1st :P
              – Max Vollmer
              15 hours ago
















            • 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
              15 hours ago








            • 3




              @glglgl Actually, the second type, right?
              – Ankur Chrungoo
              15 hours ago












            • With 0-based counting even the 1st :P
              – Max Vollmer
              15 hours ago










            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
            15 hours ago






            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
            15 hours ago






            3




            3




            @glglgl Actually, the second type, right?
            – Ankur Chrungoo
            15 hours ago






            @glglgl Actually, the second type, right?
            – Ankur Chrungoo
            15 hours ago














            With 0-based counting even the 1st :P
            – Max Vollmer
            15 hours ago






            With 0-based counting even the 1st :P
            – Max Vollmer
            15 hours ago





            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks