Printing something to the console
When you open Replit, you will see three panels. Let's talk about them from left to right;
Files
All the way, on the left, you will see a Files panel. Here, we will be able to create more Java files to store our code. That's all you need to know for now.
Editor
In the middle, you will see our editor. You should have Main.java (the only file) opened up. The editor should contain the following:
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
We will go over what this means in a bit, but for now, know this is where we edit and write our code.
Console
And finally, on the right is our console. This is where we will see our code taking place. Hit Run at the top of the screen and give it a few seconds. You will see Hello World printed. Pretty cool, huh? Now let's learn how this works.
Our first Java program
Here's the code inside Main.java:
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Let's go over it one line at a time;
class Main {
From now on, we will be associating our files as classes. So, in this case, we are looking at our Main class. When you start a Java class/file, it is essential to start with class Main {.
Main should be replaced with whatever your file is called. For example,
Program.javashould start withclass Program {
You should also see this {. This is an opening curly brace/bracket. (You can call it either, it means the same thing.) We use this to group code within it. For every open brace, there must be a closing brace.
You can see this at the end of the code, where there is a `} When we open a class, we need to close it. If we didn't do this, none of our code would run, there would be a ton of errors, and our code wouldn't be inside the class.
public static void main(String[] args) {
This next line is called our main method. It is where all of our code will go inside, and it also sends the results to the console.
For now, we won't talk about public, static, or the String[] args inside the parenthesis to make it easier now. We will talk about it in future chapters.
Just like our class, we create open and close braces, { and }, for more code to go within.
System.out.println("Hello world!");
And finally, the first actual line of code we can see. This is what prints Hello World to the console. We can actually break this line down and read it like English.
Tell the System to Output a Printed Line of "Hello World"
This is a rough translation, but you can understand it. Change the content in the "", and hit Run! Here are some examples:
System.out.println("STEMpump rocks!");System.out.println("Is blue raspberry even raspberry?");System.out.println("Is a hotdog a sandwich?");
And you will see that every time you hit run, whatever we put inside the quotes is printed. Magic, right?
Well, it isn't. You will soon learn how everything works and take your computer science skills to the next level. But for now, onward!