This example assumes you are making your first program in IntelliJ IDEA.
Your code should already include a “main” method approximately like this:
public class Main {
public static void main(String[] args) {
// write your code here
}
}
The blue text is a comment. Anything that comes after the double forward slashes will be ignored by Java, so it is only there to help the human programmer.
Delete the comment, and replace it with the following code. It has to go between the {curly braces}, just like the comment above.
Side note: you should type all of your text into the editor so you will learn faster. Copying and pasting text into your editor doesn’t activate your brain in the same way.
The capitalization, spelling, and punctuation must be correct, or it will not work. Little details matter.
Run the program in IntelliJ by pushing the run button (green arrow icon) or use the Shift-F10 keyboard shortcut.
When you run your program, it will run every command inside the “main” method, from top to bottom. In other words, it will execute every line of code that comes between the open curly brace, {, and closing curly brace, }. Lines of code between curly braces make up a code block.
Practice:
- Add more code so your program will print multiple statements when you run it. For example, make it say “This is my first program.” and then a new line to print “Learning to program takes a lot of work, but it is worth it!”
- “Break” your code in a few different ways to see what sort of error message you get. Run the code each time and read the compiler’s error messages.
- Delete a quote symbol
- Delete a curly brace {}
- Move your print command to the wrong part of the program
- Spell “System” wrong or forget to capitalize it
- Spell println incorrectly
- Forget a semicolon ;
The error messages are hard to understand at first, but you need to start learning to use them to find errors in your code. By working backwards, you can start to recognize the types of things that cause common errors.
You will be using the “println” method often, so you will want to make sure you can do it quickly and without needing to look up how to type it in correctly.