Lift off
Yay! Time to get your robot programmed. We’re going to work slowly through all the concepts separately, then piece them together once we get to roboting. (That’s totally a word.)
Setting up an IDE/Code Environment
Before we get started, you will need a code editor or IDE. IDE stands or Integrated Development Environment. It’s the place where you will edit all of your code! We will be downloading an IDE soon for all of our robot code, but for these next few lessons, we will be using an online tool called Replit. The link is above. Just sign up for an account, create a Java Repl, and we will be ready to go.
If you want to be more fancy, you can download Visual Studio Code, which is our favorite recommendation for an IDE. That link is above as well.
Writing your first lines of Java
What you’ve been waiting for. Finally writing out your first line of code. Create a file called Main.java. If you are using Replit, you should already have this file. Then, write this code in the file and hit run.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Ok, what does that mean??
It’s okay to be a bit confused, we are going to work through each line at a time.
The first line, public class Main { , contains the class name, which is Main. Notice how the class name is the same as the filename. All Java code needs to be run inside of a class. The public at the beginning means that other classes can access the content inside this class. Then at the end, we have an open bracket, {, allowing for us to put code in this class.
The second line creates a method that can be called on it’s own, disregarding the Main class. static makes sure of this. void means that this method does not return any values, so everything happens inside, and nothing comes out. This is our main method, as seen by the word main. You don’t need to worry about what’s inside of the parentheses, just know that all methods have parenthesis at the end of the name. And once again, we have an open bracket to put more code inside.
The third line is what prints out Hello World to the screen. System is a class that allows you to use useful methods and variables. out is a variable that allows the line to send an output. println is a method of out that allows you to print a line. And finally, inside the parenthesis (), there are quotes and hello world. We will explain in the next lesson why we need the quotes.
Then, you can see the close brackets at the end. These close the method and class, so the code knows where they end.