Simplest Java
October 15, 20232 min read
Write a simple program that chirps:
Birb.java
public class Birb {
public static void main(String[] args) {
System.out.println("chirp");
}
}
- Use iterm as the terminal for running commands.
- Use VSCode as the editor for simple Java program.
Java Learning Points
- When you run java, JVM looks for
main
method. static main
so that JVM can invoke the method without creating an instance of the class.- The Class name has to be exactly the same as file name and its case sensitive.
- Java is a compiled language so you have to run
javac
to generate bytecode which is the class file before you can execute the code by runningjava Birb
. This feature is intended for convenience in simple program. You should still compile your code. - Since Java 11, you can run
java Birb.java
without compiling the file. - If you define public class, you have to define static method as public.
- You can run
java Birb
orjava Birb.java
with the compiled class file, but you cannot runjava Birb.class
because it looks for class name not class file name. java
command is used to run compiled code in Java Runtime Environment (JRE) whereasjavac
command is a Java Compiler.- Java Development Kit (JDK) has Compiler (javac) and tools and libraries, and Java Runtime Environment (JRE) that libraries and contains Java Virtual Machine (JVM), which executes the bytecode.
This is Java for babies.