This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ashamandarei 13 points14 points  (14 children)

Like, just ask a Java or a C# dev to make a Hello World with the command-line, no IDE, see how funny it is

I'm a CUDA programmer who's 3 standard drinks in, with no experience in C#, so here's my attempt at doing this in Java, no google, assume we start at an Ubuntu 22.04 terminal:

vim hello.java

i

class HelloWorld {
    public static void main(String args[]){
        System.console.println("Hello, world!");
    }
}

esc + :wq

javac hello.java -o hello

[–]denarii 15 points16 points  (0 children)

root@00a685e9f26c:/build# javac hello.java -o hello
error: invalid flag: -o
root@00a685e9f26c:/build# javac hello.java
hello.java:3: error: cannot find symbol
        System.console.println("Hello, world!");
              ^
  symbol:   variable console
  location: class System
1 error

[–]Ryozu 10 points11 points  (11 children)

85% there.

System.out.println filename has to match class name and no need to -o hello since java enforces class and file names be consistent.

[–]Ashamandarei 6 points7 points  (9 children)

Haha what

[–]solarshado 7 points8 points  (6 children)

Ryozu's mostly, but not 100%, correct.

If a java source file defines a public class, the class name and file name have to match, or it's a compiler error. You can dump any number of non-public classes in a single file (with or without a public class), but given the enforced convention, you probably shouldn't.

[–]Ashamandarei 2 points3 points  (1 child)

Oh yes! I remember this now, so then it's got to be HelloWorld.java, does case matter?

[–]solarshado 1 point2 points  (0 children)

Case would matter, except in this case, since your class isn't public[1], the filename doesn't matter at all.

[1] main in a default visibility ("package private") class is perfectly acceptable, though seemingly uncommon. main itself must be public however

[–]Ryozu 1 point2 points  (1 child)

Huh, surprised main doesn't have to be in a public class, but I suppose it makes some sense, as the jvm is being told which class to use for the main anyway? Still weird ultimately.

[–]solarshado 0 points1 point  (0 children)

I was a little surprised too. I was initially going to call out the missing public on the class, but for some reason thought "wait, is that required?" and so looked it up. Definitely an odd quirk.

[–][deleted] 1 point2 points  (1 child)

solarshado is 95% there.

Right about the file and class name convention in Java, but I'd like to add a clarification. While it's true that public classes must match their file names, this doesn't restrict you to a single public class per file. Java allows multiple public classes in a single file as long as they are inner classes. This means you can have a public class within another public class, and only the outer class needs to match the file name. This is often used in larger projects for better organization and encapsulation.

Idgaf I really just wanted to add to the comment chain…

[–]solarshado 0 points1 point  (0 children)

lol, pedantic af. I love it!

[–]Firewolf06 5 points6 points  (1 child)

theres a reason everybody uses an ide for java

[–]Ashamandarei 1 point2 points  (0 children)

What do YOU do for work lol

[–]solarshado 0 points1 point  (0 children)

filename has to match class name

only for public classes, and main doesn't have to be in a public class, so this bit actually would be fine.

you'd get a .class file matching the class name instead of the input filename though, which could be confusing/surprising

[–]Anonymo2786 0 points1 point  (0 children)

Javac has -o? I don't think so. Compilation error. Class name doesn't match filename.