Click here for the previous tutorial.
Some of the most simple applications in Java are console applications. In this tutorial, than is exactly what we will be making. A console application is one that uses text over the console for communication between the user and the machine.
In this tutorial, you will learn how to display text to the console. This will be your first program you will actually be able to show off, so get some coffee and get ready for all the serious programming we will be doing today. Jokes aside, believe it or not most of the code you already have written from last tutorial, where you set up the program. Now, all that you have to do is give the computer one simple command, telling it to print to the screen. All that you have to do is put this command between the openning and closing braces of the main method.
System.out.Println("Hello World!");
Just like that, your computer is already saying hi to you. In fact, you could have made it say virtually anything by putting it between those double quotes. So this same line would work just as well:
System.out.Println("What is your name?");
Unfortunately we have no way of answering the machine just yet. We will have to come back to that.
What this means is that you now have a way of displaying any text you want to the console, easy as that. Before moving on, let's walk through some key components to this command.
First we have the three words seperated by dots. This stange syntax is actually further reference to the heirarchy, as System is the library, with a child out, which itself has the child Println (Pronounced like "Print-Lynn" or "Print-Line"). Next we have the parenthesis and quotes. This is actually the parameters of a method. There will be more on this later, for now, understand that it is where you put the needed information for the code under Println to run. In this case, it prints to the console whatever you give it. Finally, there is the semi-colon, which you will see a lot. The semi-colon works like the period of English. It lets Java know when a command is complete.
Because Java operates with semi-colons to know when the end of a statement is reached, the existence of whitespace is technically optional. This means that anywhere there is a space, a tab, or a new line, there can be anyone of those, or multiple, without the compiler seeing a problem. This also means that you could technically make an entire program on one line.
public class ClassName{ public static void main(String[] args){ System.out.Println("Hello World!"); } }
Of course, this looks hideous! Unless you are just trying to torture the other programmers you may be working with or yourself, do not do anything like this. Instead, use this freedom to your advantage, in making your code very readible. For example, add an indent when a piece is a child of another block. If it is a child of another child, then two, three, or as many indents as needed are okay. If your code works, is neat and clean, and is easy to read, then it is probably good code.
Click here to coninue to the next tutorial.
Sample code explained here:
public class ClassName{
public static void main(String[] args){
System.out.Println("");
}
}
Always remember to keep code readible!
This does not only help in large teams, but can increase productivity in small groups, and even of solo programmers. Sometimes it is even worth it just to take a little time to clean up your code so you can work better later.
Semi-colons are like the periods of Java. Remember to end each statement with one. This is the cause for a lot of syntax errors with beginners.