9 ms·
Java 21: No more public static void main
- lockhouse 3y agoThis is a nice improvement but I still think C# does this better: Console.WriteLine(“Hello, World!”); https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tu...
- Kuinox 3y agoI don't find it better. You now have an intuitive scope about what you can, or cannot do in the Top Level Statements.
- astrange 3y agoI like that this illustrates the C#/Java enterprise programming-ism that all identifiers must be in completely random namespaces to make it look fancier. Is it helping anyone that the print function is under Console? Why is an array a standard type but also a class named ArrayList under System.Collections? (Which seems to be deprecated and replaced with a class called List that's now under three namespaces instead of two.)
- fendy3002 3y agobut what is the better alternative (if any) though? In practice this can help to enable having the same Square class name, just one in MyPackage and another in SomeOtherVendorPackage.
- astrange 3y agoOne level namespaces is all you need. Anything else just encourages bikeshedding and Conway's Law issues.
- TheCycoONE 3y agoThe JEP for this is worth mentioning: https://openjdk.org/jeps/445 https://openjdk.org/jeps/445 This is a preview feature that still requires the --enable-preview flag. It's meant to reduce the initial cognitive load when teaching programming rather than an embrace of terse syntax.
- BulgarianIdiot 3y agoIf they wanted that, they'd allow top-level code, instead of this weird halfway method of doing it.
- kaba0 3y agoWhich would have zero benefits, and would require more knowledge on the exact specifics of the language. (Eg. is it a local variable of a field? The two has very different semantics).
- Gunax 3y agoSo how this works is interesting. I assumed they were just making any method called 'main' sugar for public static void. But it's not that way. Instead, apparently the proposal is to allow for main to be an instance method. Additionally, we are getting anonymous classes. Because methods can share names (both between inheritance and with different args) we apparently get a warning if a superclass also has a static main being overridden with an instance main. I would have preferred if this was simply an error.
- Traubenfuchs 3y ago> the proposal is to allow for main to be an instance method An instance method of... which instance? Is some invisible, nameless class instantiated with its default constructor? How would you acquire this main method via reflection?
- kaba0 3y agoImagine `public class [randomName] {` at the top. If it has a void main() method the class loader (not the spec!) will do new [randomName].main() You won’t acquire this main via reflection because why would you do so? It is for teaching, and one-off scripts. Later on you can trivially add that single line and name your class.
- cutler 3y agoWith all the hype about "modern" Java I still have escape regex metacharacters even after the introduction of raw string literals. Wake me up if/when Java finally fixes what really needs fixing.
- deleted 3y ago[deleted]
- re-thc 3y agoRegex doesn't seem to be on anyone's radar any time soon.
- pylua 3y agoI’m not a fan of this personally. It seems very pedantic and honestly the IDE gives such great hints on things already —- I don’t think it actually helps anyone learn anything in the long run. Also worth mentioning is that having multiple ways to do the same thing also increases complexity
- thfuran 3y agoIt seems kind of weird now that java has a repl anyways.
- re-thc 3y ago> It seems very pedantic and honestly the IDE gives such great hints on things already It's for beginners who might not even know what an IDE is.
- kaba0 3y agoBut that’s the thing, they managed to do it in such a way that it requires barely any language change: just imagine an implicit `public class X {` at the top of the file and a closing } at the end. And now just change the default class loader to be less restrictive in finding main methods, and that’s it. While it really does help teaching people when you can be more focused on the relevant parts only.
- citrin_ru 3y ago> the IDE gives such great hints on things already Requirement to use IDE is one of reasons I don't like Java. I wrote/write code in multiple languages and for all of them a simple text editor is enough. I usually don't use auto-complete because it distracts me and I type fast anyway. But with Java it's hard to be productive without an IDE.
- 9dev 3y agoI will never understand why some people actively choose to cripple their productivity. Is it some irrational pride in being able to drill without power tools?
- tyingq 3y agoI guess Java/C polyglot programs get a lot easier.
- Thorentis 3y agoWhen teaching Java a while ago, "public static void main string args" was a great way of teaching many things about programming and the Java language right off the bat. I actually liked how the cmdline args were always immediately available in the main method, unlike other languages like Python where you need to load a whole separate module to access them.
- nightowl_games 3y agoArg parse is sick tho. You'd want some module to parse the java args wouldn't you? String array isn't robust enough
- Gunax 3y agoI am assuming these are not new programmers though. When I learned Java as a first language, I remember being very confused by those words. They did not make sense to me for months. To a noob, it's a meaningless incantation that needs to be inserted for each project.
- haspok 3y ago> When I learned Java as a first language You have my sympathies
- freilanzer 3y agoAs do all students at my university where Java is not only the first language, but also used for Data structures and Algorithms, practical semester projects, etc. Many of these people finish their degrees without knowing languages other than Java and maybe Python and C/C++ exist.
- solarkraft 3y agoI don't think it's a bad beginner language at all. It's low level enough to learn about the inner workings in a structured way, but it's high level enough to not run into segmentation errors all the time. In some courses they wrote Java bytecode as a substitute for an assembly language, which seems completely sensible to me since it'll be more broadly applicable than a specific ISA. Setting up the tooling was a pain though. I did the JavaFX parts of the exam without ever having used it because I (with the help of multiple Linux nerds!) couldn't get it to run on Arch.
- armchairhacker 3y agoHere are some other features to make Java more modern: - Replace `System.out.println` with just `println` - Add operator overloading and subscript overloading for lists and maps - Add string templates with `$`, e.g. `"Hello ${user.name}!"` - Add range types, e.g. `for (i in 1..10) { ... }` - `val` keyword, like `var` but only doesn't let you assign to the variable after defining it - Add coroutines - Make `==` use `equals`, and define `===` for identity comparison - Make types non-nullable by default, and use `?` to denote nullable types. Use `!` to cast a nullable type to non-nullable, and `?.` to access a field on a potentially nullable object - Add readonly types for arrays and maps: replace `List` with `List` and `MutableList`, and `HashMap` with `Map` and `MutableMap`. - Get rid of sigils for array types, use `Array` instead. Also get rid of lowercase and uppercase variants for primitive types, just use the uppercase ones for consistency - Make it so that when a lambda is the last argument it can be outside of parentheses, when it's the only argument parentheses aren't needed, and when it has no arguments you can omit the `() =>` - Put type annotations after the variable like in most modern languages, and use the `fun` keyword for functions. So instead of `void main(String[] args)` you would write `fun main(args: Array<String>) - Put class constructor parameters on the class declaration, and allow them to be fields by specifying `var` or `val`, like `class Rectangle(var width: Double, var length: Double) {}` - `init` blocks in classes for initialization code, and `constructor(sideLength: Double) : this(sideLength, sideLength)` for alternate constructors - Add the `when` condition as a better replacement for `if else` chains. - Add convenience functions to all objects: `x.let(fun)`, `x.also(fun)`, `x.apply(fun)`, `x.use(fun)`, and `x.apply(fun)` I'm sure there are more improvements I haven't mentioned, but these would all be good first steps to make Java almost as good as other JVM languages like Kotlin
- Aaron2222 3y agoThat just sounds exactly like Kotlin?
- nightowl_games 3y agoThe first few points seem like good first steps but you soon got to a land of "never gonna happen" and then you finally just landed on kotlin itself. Why not just use Kotlin?
- 3y ago
- jbverschoor 3y agoWill this count for any method outside the class scope, but in the same file? What about constants?
- solarkraft 3y agoI very much like the idea of Java. A runtime for everything! Wow! But it feels like the coolest aspects of this idea have been realized more by .NET than by Java itself. Powershell, for instance, is so cool (in theory) because it lets you dynamically mess around with and script statically compiled components. This could (in practice it's not, since I haven't found good tooling) be so great for debugging.
- mike_hearn 3y agoI've found that it works better to use a 'real' programming language for scripts and use a UNIX shell for interactive REPL-like use. The syntax of the command line is hard to beat one you master it because it's so terse and for one-to-ten line programs like you get in a REPL maintainability doesn't matter. For anything else, it's better to write a script file and execute it. The tradeoffs are different and what works well for a program meant to stick around doesn't work well interactively. The downside of course is that if you perfect a bit of logic with a repl, you then have to translate to the other language. My company uses an in-house variant of Kotlin Scripting which has replaced bash and python for us. It's got a lot of neat features, like you can echo markdown to the terminal and it'll be rendered nicely, it has a built in progress tracking framework, it has a UNIX-like shell API for working with files and processes, you can import libraries from Maven and use them directly and so on. The only real downsides are that it's slow to compile, and currently Kotlin Scripting is still in a (very long) beta period. Hopefully when they finally release version 2 of the compiler it'll be faster and they can finally nail down the scripting support a bit better. But I have to say, it's really enjoyable to use. I used to dread shell scripting tasks because shell is just so persnickety. Now I don't. Java has something similar with jbang and jshell, but the Java syntax is perhaps a bit too verbose still. They're improving it a lot though!
- constantcrying 3y agoI don't think this is "bad", but every single person this appeals to has already switched to a different JVM language or C#. It's also somewhat of a break with tradition for Java and it's clear OOP inheritance.