Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java #35

Open
floodfill opened this issue Sep 11, 2013 · 4 comments
Open

java #35

floodfill opened this issue Sep 11, 2013 · 4 comments

Comments

@floodfill
Copy link

an empty .java is compilable by javac, or even class A{} in A.java is compilable.

@mathiasbynens
Copy link
Owner

Could you send a pull request for this?

@alrra
Copy link
Contributor

alrra commented Sep 12, 2013

Personally, I don't think this should be included in this project.

an empty .java is compilable by javac

That is because javac reads the file, sees that no class is declared, and finishes. Therefore, no .class files will be created, so there is nothing for the JVM to execute.

or even class A{} in A.java.

$ ls 
A.java
$ cat A.java 
class A{}
$ javac A.java 
$ ls 
A.class A.java
$ java A
Exception in thread "main" java.lang.NoSuchMethodError: main

@mukulgoel1989
Copy link

Yes, that is true and it is supposed to compile. The purpose of compiler is to compile the code and generate bytecode that can then be executed by the JVM

In the two cases you point.

An empty .java file : here in this case javac compiles the file, checks for syntax errors (which there are none) and then finishes compilation. No .class file is generated as the javac generates .class file for each class and anonymous classes. Since there is no class hence no .class file.

An A.java file with class A{} : here the javac compile the file, checks syntax errors (which are none) and generates an A.class file.

in either of the case, its not the compilers job to check if the .java file makes sense or not or it will give an runtime error or not. Compiler checks for syntax and compile time issues and generates bytecode if successful.

@wakingrufus
Copy link

As of Java 21, this is a valid program:

void main() {}

Reference: https://openjdk.org/jeps/445

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants