Java output is where your journey takes you next if you've been following the tutorial.
Computer programs are wonderful at making calculations, but what's the point if you can't see them? Luckily, Java output is easy to figure out.
You're going to learn the different ways of displaying results on the screen. In the previous tutorial you created a simple program that displayed hello world on the screen. This is great, however we'd probably like Java to be able to do more for us than say hello. Let's start by discussing exactly what we coded.
It only took this one line to make it display hello world, but let's look at the specifics. System.out.println() is really a piece of code that someone else already wrote. There is other code that already exists within Java that has been written for this line of code to work. You don't have to figure out how it displays whatever is inside the parenthesis, that's just how it works. By the way:
This puts a blank line on the screen, just as if you had hit enter to skip to a new line. It prints out whatever is inside the parenthesis, and if nothing is inside, it prints out nothing.
We put "Hello world!" in quotes inside of the parenthesis. This is because all words, phrases, or sentences are what we call Strings in Java. All Strings need quotes.
"Hello" <<- GOOD
Hello <<- BAD
"Hello <<- BAD
Hello" <<- BAD
There are some tricks you can do when outputting strings. For example you can add strings together, like this:
That is the same as:
The only difference here is that the string is split into two strings, and then added together (concatenated) with a plus sign. That's all there is to it, and you can break up a string into as many pieces as you want. Try experimenting with that for a bit, as well.
What if you don't want to put a new line when you're done printing out what you want? Java has a way of handling that, too.
This does the same thing but does not go to a new line when you're done. Notice how I did not leave the parenthesis empty. I had to put quotes in there because using just print instead of println requires that you put SOMETHING inside of the parenthesis. And here's another good point, two quotes right next to each other with nothing in them, not even spaces, is the same as nothing. You might as well have not written any code, because the code effectively does nothing.
Try these two lines of code:
Instead of Java placing these words on two different lines, Hello and world! Are both placed on the same line, so it reads exactly the same as what you have done before.
Why is this useful you ask? You could just write "Hello world!" the way you've done it before, so why split it up?
Here's an example. Sometimes, you may not know what you want to output. Let's say you know you want to output the world hello, but you also want to output something else on the same line. Let's pretend you do not know what that something else is yet, but you know it's based on certain conditions. Something like this:
So that part is done, but maybe you want to output the name of the user and place it right next to the hello. You may not know the user's name yet, but you want to at least output hello. If we used println, the second part of this string would be on a separate line, and that wouldn't be what we wanted. (We haven't talked about how to get input from a user, but we will get to that in a later tutorial).
That's all there is to making Java output easily onto the screen! You can modify your hello world program or create a new one and experiment with displaying different strings on the screen. Get comfortable adding semicolons at the end of each line of code, because it's extremely important not to forget them. Remember, no semicolons after lines of code that have open brackets!
If you have any questions, comments, or concerns, feel free to contact us.