6 ms·
Wow: void main(String[] args) { println("Hello world"); } I just saved that as "hello.java" and ran it (using OpenJDK Runtime Environment Homebrew (
by simonw 2y ago
Wow:
void main(String[] args) {
println("Hello world");
}
I just saved that as "hello.java" and ran it (using OpenJDK Runtime Environment Homebrew (build 23.0.1) which I happened to have on my Mac already) like this:
java --enable-preview hello.java
This is SO MUCH less crufty than the old "public static void main(String[] args)" thing. I always felt that was a terrible introduction to programming, when Hello World included a whole chunk of boilerplate and a class that didn't need to exist.
- jimbob45 2y agoI disagree. Working through each keyword until you understand the full signature felt great when learning. It’s the Python if __name__ == __main__ trash that was the worst. You’ll never understand it and it doesn’t fit in the language.
- Comma2976 2y agoIn my humble and correct (trumping anyone else's) opinion, they are both garbage.
- Quekid5 2y agoYou're not wrong... but for different reasons :)
- exe34 2y agoyou don't need it until you get to importing modules. hello world in python is literally just print("hello world!")
- dvdkon 2y agoYou don't even need it afterward, unless you want a file to be both an importable module and an executable script. Arguably you could be a happy Python programmer without this capability.
- jraph 2y agoI never understood why people bother with this when writing their python main file, which is never going to be imported because it doesn't make sense in the first place.
- elteto 2y agoBecause I dislike interspersing executable statements at module level with function definitions. I’d rather have all code live inside functions and have one main entry point for the script. It’s just a personal preference, nothing wrong with not doing that though.
- jraph 2y agoBut you could have a single main() call without the if statement, couldn't you?
- telotortium 2y agoREPL-based development. You might be developing a script, but notice a bug. If you import your script as a module, you can run the function you’re debugging, figure out what’s wrong, edit the file, and then reimport the module (using importlib.reload).
- jraph 2y agoOkay, that's quite convincing, thanks!
- usrusr 2y agoIsn't that hugely elaborate compared to languages where top level statements are a function, functions return their last statement, and the return value of the entry function is sent to stdout unless invoked in special ways? That print() around the "hello world!" does not appear any less superfluous than all the public static void you can throw at a javac 1.0
- driggs 2y agoYou mean a language like... Python? >>> 'hello world' 'hello world'
- usrusr 2y agoExactly like that (well, almost exactly: if python includes the quotes then it's not really equivalent to print()). The point was that by including that print(), people already do say yes to including "like you'd do in a real program" ceremony in their supposedly minimal examples.
- dajtxx 2y agoPython seems to have started with 'why do those other languages have all this trash' and then spent the time since then learning why and coming up with worse versions. I use python a lot these days, and like it, but it's pretty funny seeing stuff like the above and type hints. I hate not knowing what types a function takes and returns.
- nayuki 2y ago> Python seems to have started with 'why do those other languages have all this trash' and then spent the time since then learning why and coming up with worse versions. This seems true to me too. Examples: * if __name__ == "__main__": main() * Private members being marked by convention with leading underscore (e.g. def _foo()) instead of being a language feature * @staticmethod as a decorator instead of being a language feature * Duck typing vs. abstract base classes * Static type hints got retrofitted to the language gradually, one feature at a time * Reference-counted garbage collection seems to be more deterministic than tracing garbage collection and ensures that unreachable resources have their finalizers run as soon as possible... except it's not true * Having a GIL instead of being truly multi-threaded from day one * Various OOP concepts that are much better explained in Java than Python: __eq__(), __hash__(), monitor wait() and notify(), object finalizers, thread safety, weak references * Distinction between str and bytes. This is the biggest change from Python 2 to 3 and caused a lot of incompatibilities. Java separated String and byte[] from the start (though the use of UTF-16 is unfortunate).
- Quekid5 2y agoThe whole switch/pattern matching thing is a true abomination borne from: Well, things in statement position can't be expressions (or however Pythonistas might phrase it), so clearly switch/pattern matching must be a statement... It's such an own goal and undermines the main point of pattern matching. (I do realize that there was opposition on this, but that the Clearly Wrong side won out is baffling.)
- nayuki 2y agoYeah, it's a shame for Python. I am extremely impressed that Java 14's switch expression ( https://openjdk.org/jeps/361 https://openjdk.org/jeps/361 ) is a generalization of the switch statement (since Java 1.0); it's like they took a page from Rust's language design. I have found the switch expression to be quite useful in my work.
- p1necone 2y agoReally Java has always been kinda okay - the really horrible part of Java was always the "enterprise OOP" programming patterns that were basically a given in any commercial codebase - little things like pointing out the verbosity of a basic single file project are just shorthand for "look at all this shit people have written". As the adage goes - Java programmers can write Java in any language.
- Terr_ 2y ago... But if they do make it in Java, at least you have the tools to analyze and refactor it.
- bzzzt 2y agoYou mean stuff like in the 'Design Patterns' book by the Gang of Four? That was originally written for C++ programmers and predates Java by a few years. Seems 'Java programmers' were already writing Java before Java's release ;)
- fuzztester 2y agoPython programmers be like: from __past__ import JavaProgrammer /usr/bin/python: error: line 0: not a chance
- watwut 2y agoThat is something yet different.
- deleted 2y ago[deleted]
- trallnag 2y agoIs the problem design patterns or opaque frameworks that rely on a ton of reflection?
- watwut 2y agoAfaik, at the time issue was a lot of programming in xml - where those frameworks are configured in various xmls and hard to understand and maintain. Reflection based frameworks are later development.
- jjice 2y agoAnecdotal, but I had an intro to programming class in high school and the teacher was a wonderful woman, but not a programmer. She didn't know that this was a function declaration or its meaning, so she would just have us repeat out loud "public static void main string box args". I get a laugh whenever I think of that. I hope she's doing well.
- keyle 2y agoI had an Excel class 20-25 years ago where a teacher explained click and double click every class, and emphasising "DOUBLE CLICK" out loud every time she did the action... There were a few facepalms. The next class was assembly programming, where the teacher didn't bother to show for 4 months and then resumed the class as if we "self-taught up until this point". We were utterly lost in that one. I imagine things have changed greatly today but back then it was a complete roller coaster.
- ElevenLathe 2y agoIn elementary school we had a "class" with the "computer teacher" once a week. He was just one of the only adults on the school district payroll who knew how to turn on and off an Apple //e. Nice guy (he also volunteered at the same community theatre I did), but "computer class" was just letting us loose with copies of Fraction Munchers and helping us if we jammed the disk drive or whatever.
- nickpeterson 2y agoI remember being 10 and going to a computer lab full of power macintoshes and playing sim city 2000, kid pix, and Carmen San Diego. This was in between looking at a cd encyclopedia (encarta?), old computers were awesome ;)
- hobs 2y agoWow, you reminded me of two experiences at once. I wasn't good at getting educated, but I ended up at a community college to "get my generals" - my first class of the day was computer something or other, and the class was led by a man in extremely thick glasses who said that our general approach in the course was going to be: * perform actions with the computer * take a screenshot * paste the screenshot into mspaint * print out the screenshot as proof that we had completed the thing * put that in a folder * he would review the folder for completion (for anyone paying attention, anyone could just print out multiple copies) While it was a significantly younger and less experienced me who quit this bullshit in an absolute huff, I don't know if I could go through it today. Fast forward a few years later, and I was training folks on tech support(a random reversal) and I had a lady who should have taken his class... every time I told them to click on something she asked "is that right click or a left click?" and each time I would respond "its a left click unless I tell you otherwise" (she didn't last long.)
- riku_iki 2y ago> than the old "public static void main(String[] args)" thing you forgot surrounding class definition.
- geokon 2y agoI haven't written Java in a million years.. so I'm trying to understand better. But what does this code even mean..? Isn't Java's whole simplifying model that everything lives in "Objects"? This seems to break that fundamental paradigm and makes the language more complicated. If you needed something like global functions then in vanilla Java as I remember you'd stick it into static final Objects that acts as a namespace of sorts. So you don't need to worry about functions floating around in a global namespace soup. If you're gunna just have free floating functions in your file.. then why not just cut to the chase and have a println("Hello world"); floating in your file..? (I mean... you don't have REPL so maybe that wouldn't be too meaningful...)
- int_19h 2y agoIt's implicitly still a class, you just don't need to write all the scaffolding to indicate that. The functions are still methods of that class etc. But, yes, with static imports, you can just write "println" like that and have it work.
- almostgotcaught 2y ago> It's implicitly still a class, you just don't need to write all the scaffolding to indicate that. did you know that every function in a python script is actually an attribute of that module (which is basically a class). so my point is: who cares.
- imtringued 2y agogeokon's point still stands. This is meaningless syntax sugar that does not change anything fundamental about Java except add one more special rule into the language aka it is one more thing to learn. Rather than being helpful, it just becomes friction when an old developer uses the old method and a new developer uses the new method. You still need to know what a main class is, so that you can reference it in your build.gradle file.
- losvedir 2y agoIf you're using a build.gradle file then you probably won't see any benefit to this implicit class, yes. But it's useful for little one-off scripts, where you just go `$ java foo.java` from the command line.
- foobarian 2y agoSong version: https://www.youtube.com/watch?v=yup8gIXxWDU https://www.youtube.com/watch?v=yup8gIXxWDU
- globalnode 2y agowhat did i just witness....
- klez 2y agoOne of the best metal band Italy has on offer, that's what you just witnessed. SEMICOLON!!!
- AiTricky 2y agoThanks for sharing this. I watched it while both enjoying and cringing at it.
- Phelinofist 2y agothis is actually fucking amazing :D
- rottc0dd 2y agoI agree that can serve as terrible intro to programming. But, often when we have tools either designed for a purpose in mind or a dominant paridigm or reaction to existing set of tooling, this can result in understandable yet extreme abstractions. Java is designed with OOP in mind and it kind of makes sense to have the user to think in terms of lego blocks of interfaces. Every method or class needs to have clear understanding of its users. public - software handle is for all users protected - software handle for current and extending classes default - software is exposed to current package private - software is restricted to be used in current class alone and nowhere else So, the beginning of java programming starts with interface exposed to the user or other programmers. Is it weird and extreme. Yes. At least, it is consistent.
- brokenmachine 2y ago>less crufty than the old "public static void main(String[] args)" thing. I always felt that was a terrible introduction to programming I somewhat agree, but it was kind of exciting to learn what each of those previously meaningless words meant. Kind of a sneak preview... Tune in for next week when we learn what static means!!
- fulafel 2y agoIt's a good local improvement, but the other hand that old traditional syntax was a good effective "keep away" sign in Java that saved many CS students from being fed Java as a first language. And it's still a bad first language after this change. I wonder if this small improvement might make things worse in the big picture.
- thayne 2y agoAnd yet it is still the first language taught in many programs. Although my high school programming class was c++, which is even worse as a first language.
- user_of_the_wek 2y agoThese features are explicitly to make teaching Java as a first language easier. From https://openjdk.org/jeps/463 https://openjdk.org/jeps/463 Goals: Offer a smooth on-ramp to Java programming so that instructors can introduce concepts in a gradual manner. Help students to write basic programs in a concise manner and grow their code gracefully as their skills grow. Reduce the ceremony of writing simple programs such as scripts and command-line utilities. Do not introduce a separate beginners' dialect of the Java language. Do not introduce a separate beginners' toolchain; student programs should be compiled and run with the same tools that compile and run any Java program.
- themaninthedark 2y agoI learnt to program using Pascal, then C++ in Highschool. In collage, CS101 and 102 required me to learn and use JAVA, which I felt was a huge step backwards in usability and a lot of excess typing. But as I reflect on it now, I think my feeling were because I came from knowing how to do some things and therefore not requiring the framework that JAVA offed to first time programmers.
- fulafel 2y agoI think the sins of Java as a first language are twofold: 1. The obvious excess incidental complexity 2. and more important, the enterprisey culture that's much less compatible with the intellectual curiosity, looking under the hood, etc attitude that's needed to become a decent programmer.
- BeefWellington 2y agoThey've also done quite a bit of work to make things such as hosting a web service much much simpler, through the addition of things like SimpleFileServer, as well as some better convenience methods. It makes it far far closer to the python experience.
- user_of_the_wek 2y agoYou don't need the args! void main() { println("Hello world"); } should work just as well.
- jillesvangurp 2y agoYou might also like Kotlin script. #! /usr/bin/env kotlin println("Hello world") You have to brew install kotlin for this to work of course. But it's a great way for using a lot of Java stuff as well. Kotlin's Java interoperability is excellent if you are using Java from Kotlin. IMHO Kotlin is underused as an alternative to python currently for data science stuff. It's surprisingly capable out of the box even with just the standard library and there are a lot of nice data science libraries that make it more useful. Not for everyone; but fairly easy to get started with. Kotlin scripting is unfortunately not necessarily very user friendly (e.g. imports can be a bit tedious and IDE support is a bit meh). But it can be a nice way to embed some kotlin stuff in a script. Generally, Jetbrains could give this topic a lot more love and attention and it wouldn't even take that much to level up the experience. KTS works in jupyter as well (there is a kotlin engine for that). And that of course is nice if you want to use Java libraries in jupyter. And developing kotlin DSLs for stuff and then using them in a script is kind of a power move.
- a57721 2y ago> Kotlin scripting is unfortunately not necessarily very user friendly (e.g. imports can be a bit tedious and IDE support is a bit meh). But it can be a nice way to embed some kotlin stuff in a script. Generally, Jetbrains could give this topic a lot more love and attention and it wouldn't even take that much to level up the experience. Kotlin has been pretty bad at scripting and REPL, and unfortunately the team decided to drop both: https://blog.jetbrains.com/kotlin/2024/11/state-of-kotlin-scripting-2024/ https://blog.jetbrains.com/kotlin/2024/11/state-of-kotlin-sc...
- GTP 2y agoBased on that page, they're dropping the REPL in favor of notebooks, but their're not dropping scripts. They are dropping some script-related functionality to focus on others.
- jillesvangurp 2y agoAlso worth noting that notebooks and kts overlap quite a bit. They use the same mechanism for imports and defining remote repositories, for example. A notebook is effectively a kts script. And of course gradle uses kts as well for its Kotlin dialect. These things actually overlap quite a bit.
- pragma_x 2y agoIt took almost 30 years to get here. I never thought I'd see the day. What really threw me for a loop is the article author mentions: "I knew plenty of professors who were bothered by [the public static void main thing]." I could have sworn that OOP die-hards in academia _wanted_ the formal class boilerplate in the original design. Maybe they aged out? Perhaps, times are changing.
- belter 2y agoOne is void main(String[] args) { println("Hello world"); } and the previous one ... class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } And that is SO MUCH less? Are you being ironic? Should we allow somebody who cannot handle the second one, go on and programming because of the first one?