In order for you to go on, you must have your IDE (preferably Eclipse) and the JDK all set up. Now, we can begin coding!
Open up Eclipse. Once it has opened, go to the top left corner of the screen, and click on File. Then scroll over New, and select Java Project. You can name your Java project whatever you like, but I'll choose to go with MiiverseTutorial. Continued...
I recommend that you save it into a folder which you can use to store your programs. Try making a new folder in your hard drive, like this: C:\MyPrograms.
After you have created your project, it will show in the Package Explorer on the left. If you click on it, you'll see something called src. Right-click on src, scroll over New, and select Class. Name your class "HelloWorld", and click finish.
Hooray! We can now TRULY begin to write our code!
In the editor, you'll see something like this:
public class HelloWorld {
}
If you have absolutely no idea what this means, have no fear. I will explain it all right now.
A class is a blueprint from which objects are created. It could be considered as a category, which I'll explain in the next comment.
For example, let's say there is a class named Shape. Circle, square, and triangle are all objects of the Shape class.
Or, how about a Cat class? The Cat class can contain objects such as catnip or whiskers, but can also have "actions" such as purr and rip your couch apart.
These actions are known as "methods" in Java. I'll talk about objects and methods later in this course.
Java programs MUST ALWAYS be written inside of a class. Our class HelloWorld is the class we will be using to write our code...for now. However, we won't be creating any special objects in this class.
The keyword public basically means that the program can be accessed by other programs outside of the class. If you'd like, it can be private, which means that it can't be accessed by outsiders, or protected, which means it can only be accessed by programs in the same "package." The package in our MiiverseTutorial project is called src, and it looks like a brown box. Continued...
So if you were to change public to protected in our newly made class:
protected class HelloWorld {
}
Then only programs within src can access it. Cool, huh?
And finally, let's go over those curly brackets.
The programs are written between those two curly brackets, which I'll refer to as braces from now on. IMPORTANT: You must always have an opening brace to begin the program and a closing brace to end it, or else you will get an error.
{: Opening brace.
}: Closing brace.
Alright, now click on the right side of the opening brace and press Enter.
It will go to the next line, but you'll notice that it has an indent. Why is this?
This special indent is called "whitespace." The purpose of whitespace is to make your code easier to read and to make it more organized. If you've seen other people's programs, you will probably see some examples of whitespace. Feel free to start at the left margin, though. It won't mess up the program.
Alright, now on the second line, type this line in:
public static void main(String[] args)
You probably have no idea what this line of code means, but do you see something familiar? Yes, the keyword public! This special piece of code can be accessed by other programs.
But what does static mean? What does void mean?
If you can bear with me here, I'll explain those other keywords when we go over methods in a later tutorial.
This line of code that we have just wrote creates, or "defines", a method called main. EVERY Java program MUST contain a main method. The method name goes right before that opening parenthesis, and everything before it is all keywords.
When writing a method, you must ALWAYS place a pair of opening and closing parenthesis next to the method name. The stuff that goes inside the parenthesis is called the "parameter" or "argument." You can have more than one argument when defining a method. The argument for main is:
String[] args
Once again, we'll find out what this piece of code means later.
After the closing parenthesis, place another opening brace. Your program should look like this now:
public class HelloWorld {
public static void main(String[] args){
}
}
We have created another pair of braces! So we can write our program inside our main method, which is inside our HelloWorld class. We're going to do the fun part now!
Under the line where we defined main, type this one-lined wonder in:
System.out.println("Hello world!!!!!");
The System.out.println command is used to "print" things to a console. No, the message "Hello World!!!!!" will not come out of your printer. Within the parenthesis you must type in the message between double quotation marks.
Anything between a pair of double-quotation marks is called a string, a data type we will go over later.
You'll also be wondering what that semicolon is doing at the end of that line. Semicolons are used to "terminate" single-line statements, just as a period terminates an English sentence at the end.
Here's the final program:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world!!!!!");
}
}
Once you have finished writing the program, click that green play button near the top of the screen. And voila! Your message will be printed to the console below!
This concludes Java tutorial #1. Feel free to discuss what you have learned here!
I've been wondering something for a while: Is it possible to have the text in System.out.println(""); divided up on to separate lines without multiple println lines?
You can use only one System.out.println to print out one message, but that message can be typed up on different lines! Like this:
System.out.println("This string "
+ "can be combined "
+ "with other strings "
+ "using addition?!");
And the output would be this:
This string can be combined with other strings using addition?!