Anil Philip wrote:Why can't it take it as String?
Nor is it a String constructor. The nearest I would say is, it is an expression that calls the constructor.Last night, I wrote:String::new, as you doubtless already know, is not a String . . .
I have two examples, which both compiled and ran correctly. In the second case, it shows that the new String is a copy of the original using the == same object operator.Or maybe several functional interfaces. . . .
If I can find two interfaces that will act as targets for that λ expression, how would you expect the compiler to know which you want?My JShell wrote:jshell> Function<String, String> f = String::new;
f ==> $Lambda/0x00007f37fc00a200@9807454
jshell> Supplier<String> s = String::new;
s ==> $Lambda/0x00007f37fc00a638@b1bc7ed
jshell> System.out.printf(
...> """
...> String produced = "%s"
...> """, s.get());
String produced = ""
$5 ==> java.io.PrintStream@64a294a6
jshell> List<String> words = Stream.of("Campbell", "Ritchie").map(f).toList();
words ==> [Campbell, Ritchie]
jshell> void foo()
...> {
...> List<String> words = Stream.of("Campbell", "Ritchie").map(f).toList();
...> System.out.println(words);
...> System.out.printf(
...> """
...> words.get(0) == the literal "Campbell": %b
...> """, words.get(0) == "Campbell");
...> }
| created method foo()
jshell> foo()
[Campbell, Ritchie]
words.get(0) == the literal "Campbell": false
There are three kinds of actuaries: those who can count, and those who can't.
Campbell Ritchie wrote:The nearest I would say is, it is an expression that calls the constructor.
Campbell Ritchie wrote:If I can find two interfaces that will act as targets for that λ expression, how would you expect the compiler to know which you want?
Anil Philip wrote:For Generics expressions, there are rules where if there is an ambiguity then the compiler defaults to a certain type like Object
Stephan is right; you are more likely to get a warning or for the code to fail to compile.Anil Philip wrote:. . . For Generics . . . if there is an ambiguity then the compiler defaults to a certain type like Object . . .
When you put <XYZ> anywhere in your code, you are instructing the compiler to go into fusspot mode and complain at the slightest ambiguity of types. The code will be completely type‑safe, iff there are no compiler warnings and @SuppressWarnings is only used where the programmer is absolutely sure the type is correct.. . . why the compiler couldn't apply some default rule . . .
It is very difficult to write good compiler error messages.the error message isn't clear.
Campbell Ritchie wrote:Stephan is right; you are more likely to get a warning
When you get a ClassCastException, you will not think it is good.Anil Philip wrote:. . . A warning is still good - it will compile and run. . . .
Stephan van Hulst wrote:
Anil Philip wrote:For Generics expressions, there are rules where if there is an ambiguity then the compiler defaults to a certain type like Object
Please do try to think of an example, because this doesn't sound correct.
Stephan van Hulst wrote:It turns out that many developers not only want their language to be object oriented, but they also want a strong type system with covariant and contravariant generics and functional programming with lambdas and currying.
I suggest you learn Java® properly first. As Stephan says, the type‑safety is a lot better.Anil Philip wrote:Is Python the same way? (I need to learn it next). . . .
Stephan van Hulst wrote:If you want your application to run forever, Java is a very good choice.
Campbell Ritchie wrote:I suggest you learn Java® properly first.
Stephan van Hulst wrote:I've never held programming certifications in high regard. Studying for a certification is not going to teach you how to be a good programmer. Instead, you need to program. Work on a project. Make mistakes. Ask questions.
I'm not saying a certification is useless: many companies seem impressed by them and with certifications on your resume it's easier to get a foot in the door. But personally I think they're a poor measure of actual skill.
Paul Clapham wrote:I have an application which I use frequently, written in Java. I first wrote it in about 2005, I think -- it replaced a version which was written in Turbo Pascal. The code I have now could never have been written in Turbo Pascal or any of its follow-on products, I'm sure. But it could be written in Java because Java has absorbed so many new features over time.
Anil Philip wrote:
Paul Clapham wrote:I have an application which I use frequently, written in Java. I first wrote it in about 2005, I think -- it replaced a version which was written in Turbo Pascal. The code I have now could never have been written in Turbo Pascal or any of its follow-on products, I'm sure. But it could be written in Java because Java has absorbed so many new features over time.
Could you get that program to run in Java 11 without any code refactoring?
Paul Clapham wrote:It's running in Java 21 at the moment. I've done a lot of changes to it in order to use new features of Java so yes, if I wanted it to run in Java 11 I would have to refactor it by removing those changes. I can't imagine why I would want to do that though.
Anil Philip wrote:I meant, would the code originally written in 2005 run in Java 11 without refactoring? I think you will agree that it will not.
Paul Clapham wrote:I wouldn't agree with that at all. I'm not aware of anything that I would have had to rewrite.
Anil Philip wrote:
Paul Clapham wrote:I wouldn't agree with that at all. I'm not aware of anything that I would have had to rewrite.
Perhaps this does not apply to your code https://docs.oracle.com/en/java/javase/11/migrate/index.html#GUID-4B3D2D73-359C-4ADA-937E-BAEA79CFDF0F
Stephan van Hulst wrote:That's a very narrow perspective.
Anil Philip wrote:I only see
Another example, IO and NIO2. They couldn't just scrap IO and replace it with NIO2. They keep both.
Grow your own food... or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|