Outside Resources:
TutorialsPoint Hello World
TheNewBoston Hello World
learnjavaonline.org Hello World
Open BlueJ Software.
(download and install BlueJ if running on your own computer)
From the Project menu, click New Project.
Choose a folder to save this project and give it a reasonable name.
Click the “New Class” button.
Give the class a name:
- No spaces in the name
- Start the name with a capital letter
Click the icon that appears, and you will see some pre-written code.
You can clean this up by deleting text so it just looks like this:
public class FirstProgram { public static void main(){ } }
You can delete everything between the first and last curly bracket, then type the part that says “public static void main(){}”
This is about as simple as a Java Program can be.
It doesn’t do anything yet, so we will add a command to print some text:
public class FirstProgram { public static void main(){ System.out.println("Hello World"); } }
At this point, you can try to compile your program to see if it has errors in the code.
Click the compile button at the top of your text editor.
At the bottom, it should say “Class compiled – no syntax errors.”
If it has an error, you need to figure out how your code is different from the example and fix it! Look for small details that are wrong.
When it compiles, go back to your main BlueJ screen with your class icon box.
Right click that icon and click “void main()”
This should run your program, which will display “Hello World” in an output window.
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!” (each of these command ends with a semicolon;)
- “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.