Head First Java - Chapter 01 Breaking the Surface
How java works? The goal is to write one application and have it work on whatever device you have.
Source is the .java extension file. Compiler create a new document by converting the source file and it is called byte code. Virtual machines reads and runs the byte code which created by compiler.
Code Structure of Java
Source file holds the class information and it represents a piece of program. Class file has one or more methods. Inside each method has one or more statements. Statements can be declarations, assignments, method calls etc.
Anatomy of a Class
When JVM(Java Virtual Machine) starts running, it starts looking for a specific method which is the main method. JVM runs what’s inside the main method. Every java application has at least one class and have at least one main method.
Writing a class with a main
In java, everything goes in a Class. You type your source code and save it (.java extension) and then compile it into a new class file(.class extension). Then you run the program and you’re really running a class.
MyFirstApp.java
public class MyFirstApp{public static void main(String[] args){system.out.println("My first Java Program");}
}
Compile
javac MyFirstApp.java
Run
java MyFirstApp
You will see you got output as “My first Java Program” on terminal.
What can you say in the main method?
You main method can have,
- Statements - declarations, assignments, method calls etc.
- Loops - for loop, while loop
- Branching - if/else tests