“new” Keyword In Java Lambda Method Reference [duplicate]
up vote
8
down vote
favorite
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;
java lambda java-8 method-reference
marked as duplicate by Federico Peralta Schaffner
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.
add a comment |
up vote
8
down vote
favorite
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;
java lambda java-8 method-reference
marked as duplicate by Federico Peralta Schaffner
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, sos -> ""
will do the same thing
– Michael
Nov 21 at 12:49
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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;
java lambda java-8 method-reference
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
java lambda java-8 method-reference
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
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
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, sos -> ""
will do the same thing
– Michael
Nov 21 at 12:49
add a comment |
4
a new String in upper case is still just a blank string, sos -> ""
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
add a comment |
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();
add a comment |
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)
Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
1
Strictly speaking,new String()::toUpperCase
is indeed a method reference, of the third kind (new String()
is an object which has thetoUpperCase
method). It doesn't take arguments, but returns aString
. It could be used as aSupplier<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
add a comment |
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();
add a comment |
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();
add a comment |
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();
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();
edited Nov 21 at 12:56
answered Nov 21 at 12:51
Eran
274k35438521
274k35438521
add a comment |
add a comment |
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)
Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
1
Strictly speaking,new String()::toUpperCase
is indeed a method reference, of the third kind (new String()
is an object which has thetoUpperCase
method). It doesn't take arguments, but returns aString
. It could be used as aSupplier<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
add a comment |
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)
Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
1
Strictly speaking,new String()::toUpperCase
is indeed a method reference, of the third kind (new String()
is an object which has thetoUpperCase
method). It doesn't take arguments, but returns aString
. It could be used as aSupplier<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
add a comment |
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)
Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
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)
Refer: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
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 thetoUpperCase
method). It doesn't take arguments, but returns aString
. It could be used as aSupplier<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
add a comment |
1
Strictly speaking,new String()::toUpperCase
is indeed a method reference, of the third kind (new String()
is an object which has thetoUpperCase
method). It doesn't take arguments, but returns aString
. It could be used as aSupplier<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
add a comment |
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