public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
This is your first ever program in Java. It will print:
Hello, World!
I wanted this to be your first program so I can stuff a few important things into your mind right away.
The first two lines and the last two lines (greyed), think of them as a magical spell you must write. Write the first two grey lines at the start of your code, and the last two grey lines (two closing brackets) at the end of your code. Don’t worry about their meaning yet.
The third line (golden), this is where you’ll be writing most of your code for now. That’s the line that actually does something. Now replace the Hello World with anything and it will print that.
Now, here are 3 things you must understand from this program:
1. File Name vs Class Name
In the first line, the third word after public class
must match your file name.
If your file name is HelloWorld.java
, then the third word must be HelloWorld
If your file name is Monkeys.java
, then the third word must be Monkeys
Even capital and small letters matter. Banana and banana are different for Java.
2. The main Method
In the second line, you’ll see the word main . This is the starting point of your program. Whenever you run code, Java looks for this keyword and starts executing from here.
3. Curly Brackets Around the Main Block
Right before the golden line, a {
curly bracket opens, and right after it, a }
curly bracket closes. Everything inside these brackets is called the main method. Think of it like the main box of code where you’ll be focusing for now. You will deal multiple boxes of codes which are also called methods each enclosed in curly brackets. But your program will always execute the main Method first
CLOSING
That’s it for the your first java program!
In the next blog, we’ll understand Variables and Data types.
0 Comments