Java 21: No more public static void main
twitter.com
twitter.com
- 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
Many of you other points are also purely stylistic (eg. types before or after parameter/variable names), these will not change just because you don't like it.
Kotlin is not the panacea of language design either.
Your suggestions are basically all just meme features from nobody languages. Java is the king for a reason.
Of all the reasons Java is popular, ergonomics is absolutely not one of them.
There's a single other kind of concurrency: unstructured concurrency. Which is where you just start threads/goroutines/coroutines/etc and they just... all do their own little thing ? They can return at any time, have no parents, etc.
Structured concurrency is about making a tree of concurrent processes. You may have a parent job, that has children. Cancelling this job will cancel all its children. For example, you may have a job that's linked to an UI (so it needs to stop the moment that UI is gone), to a socket (so it needs to stop the moment the socket closes), or to nothing and is a global scope (that will keep running as long as your OS doesn't exit the process). These scopes can in turn have their own children and their own scopes. And maybe you have a task way, way down in your tree that spawns two tasks: one is a long running task that must exit the moment its parent stops ("the user clicked on cancel"), one is a long running task that must stay alive as long as necessary ("the user's data is being uploaded to a server in the background"). With unstructured concurrency, you can't really express that. With structured concurrency, just pass the appropriate scope down.
Also worth mentioning is that having multiple ways to do the same thing also increases complexity
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.
What to some extend can replace debug logs is tracing which allows to logs events on demand, when you really need them. E. g. Dtrace can be used both for kernel and userspace via USDT (but Dtrace was not invented h̵e̵r̵e̵ on Linux), bpftrace can instrument kernel but unsure about userspace.
A few minutes for the IDE to open? Nah, bruh. Couple of seconds, and since you leave the IDE open for a while, that startup time gets to be ignored. You aren't starting the IDE, exiting the IDE, running your code, starting your IDE, exiting the IDE, running your code.
You're starting the IDE, writing, running, debugging, reaching some kind of finishing point, THEN shutting down the IDE... maybe. A lot of programmers have their IDEs open 24/7. Over a month's runtime, those three seconds opening the IDE seem (and are) irrelevant.
Billion GUI windows? Nah, bruh. Even IDEA, which has a lot of windows on initial installation, has only a few... and they're all for a point, if you, like, read them.
A second to process your keystroke? Also nonsense. Even a raspi can process faster than that. Unless you're deliberately overloading your machine, I suppose. That's always an option... but a counter to that is "... don't?"
And useless toolbars? Useless to whom? The IDEs you'll find in Java are battle-tested. The useless toolbars are hidden these days. You might not know what they're useful for, but that's not the same as being useless. If you write Java, you'll learn how useful they are.
And as a bonus, you'd ... did you say.. have to pay more money than your dorm costs? ... why?
Java has three major IDEs: IDEA, Eclipse, and Netbeans. (If you want to exclude netbeans, join the crowd. Everyone else excludes it, too.) All three have free editions that cost you ... like... literally... nothing. Zip. Nada. Zilch.
Try again.
You can take vim out of my cold dead hands but there’s no way one will be less productive in Java with modern IDEs unless they willingly choose to
Having an IDE is like hiring a side-kick --- if the side-kick is component enough and you can afford them, why wouldn't you hire one?
> if the side-kick is component enough and you can afford them, why wouldn't you hire one?
Because it doesn't provide anything actually useful for me.
Also, I don't want to use proprietary software for moral reasons.
There are many decent open source IDEs (e.g. NetBeans and Eclipse for Java).
I mean, more seriously, I get it: you have a bee in your bonnet and you will find any reason possible to avoid it. VIM 4EVA and all that, raise your fist and yell.
But I wouldn't even bother with Java as any serious endeavor if I were you; a lot of employers will ask your preference of IDE just to get a sense of who you are as a programmer, and will hide their giggles when you say "I use emacs/vim/notepad++ like a BAWSSSS!" and move on to the next candidate who'll get the performance magnifier of a common and free tool.
What a strawman. I don't use Neovim with the intent of looking cool or something, but because it makes me highly productive, gives me complete control over my editing experience and I can use it anywhere, not just on powerful desktop computers with a large screen.
Exactly.
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.
It makes an analogy to packages. We allow classes to exist in the default package. Therefore, I think it's reasonable to allow methods to exist in a default class.
Of course you shouldn't so this in real work, but that's also true of the default package.
I also like that main doesn't need an argument. Many noob programs don't even use the String[] args parameter anyway, so let's not require it.
It's for beginners who might not even know what an IDE is.
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.
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.
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?
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.
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.
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.
I have been on the same path.
You have my sympathies
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.
(Some distros may even package a JDK with bundled javafx).
Console.WriteLine(“Hello, World!”);
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tu...
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.)
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.
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!
What about constants?