I have a dilemma. I believe in teaching something small, and then building on this, and building on this some more, and voila, you get to a Java program. Unfortunately, the way I originally wrote this tutorial, you'd spend 20 lessons before getting to your first working program.
This is the rough equivalent of teaching someone how to drive by telling them the various parts of the car (steering wheel, accelerator, etc), the safety rules of the road, etc. Halfway through, you may have learned a lot about driving without driving.
If this would help make you a better driver, would you care? Or perhaps it's best to just drive now, and figure things out.
Since I don't know which way is best, I'm going to introduce you to a Java class right now. You won't exactly know what anything means right away, but that's OK. It's going to be painless to learn, and you'll eventually see the pieces fall into place.
We're going to do the equivalent of teaching you how to drive a car, even when you don't know what anything in a car does (pretend you haven't been in a car all your life and have no idea what a steering wheel does or the blinkers do, or anything). In the meanwhile, you'll also be doing the equivalent of learning what a car does. Eventually, the two will meet, and what I write below will suddenly make sense.
For now, prepared to be somewhat confused.
Each class has a name. If you write a class, you get to come up with its name. Suppose you pick the name Rectangle.
To write a class, you must use some kind of text editor. A text editor is basically a word processor. It allows you to type your program. A good text editor may even recognize certain parts of Java and do things to make your job of typing the program simpler.
When you write a paper using a word processor, you must name the file. You must also name the file when you edit a Java program.
Java has strict rules when it comes to the name of the file. If the class you are writing is named Rectangle, you must name the file Rectangle.java. If the class you're writing is named BankAccount, then the file must be called BankAccount.java.
The pattern is simple. Whatever you name the class, you must name the file the same name as the class, and add a .java (read that as "dot java") at the end.
public class Rectangle
{
}
Here's what you type:
Everything between the open and close brace is the class definition body. So far, this body has nothing in it.
As practice, write the stuff above, but change the name of the class to BankAccount.
What goes into a class definition? Methods. What's a method? Ah, that's yet another thing you'll learn later on.
We'll put a main() method in. It's shown in navy to make it stand out more.
public class Rectangle { public static void main( String [] args ) { // main's method body } }The first open brace you see is the start of the class definition body.
Type the following:
Be careful. All words except String have only lower case letters. String starts with a capital S. Make sure you have the parentheses too!
This is the start of an important method called main(). Every class can have one such method. Say it aloud a few times "public, static, void, main, String, brackets, arg". Repeat, and again.
You say this has no meaning to you? That's OK. For now, all you have to do is to be able to write it down exactly as shown. Eventually, you will learn what each of the words mean.
public class Rectangle { public static void main( String [] args ) { // Printing a message System.out.println( "Java Rocks!" ) ; } }We've added the following line to the main's method body.
System.out.println( "Java Rocks!" ) ;Type this accurately. You must use the correct capitalization, dots, double quotes, and semicolon at the end. Any little mistake and your program may not run.
Save this program, and quit out of the editor.
How do you compile? If you have Java installed correctly, and have a prompt, and work in a UNIX environment (I know, this may not describe your situation), then type:
javac Rectangle.javaThis compiles the program. javac (which is java, followed by the letter c) is the name of the compiler. Rectangle.java is the name of your Java file. If it successfully translates, the Java compiler creates a file called Rectangle.class. Your original file is still there, however.
If you have typed something incorrectly, it may print error messages to the screen, and you need to fix it. If this happens, start up the editor on the file, reread your program, and see if you can spot any errors. If you can't, then ask an expert to help out.
Suppose there are no errors. Type the following:
java RectangleThis time, you only type java. There's no c at the end of this one. You also just type the name of the class. You don't type .java or .class at the end.
If all goes well, you should see:
Java Rocks!on the screen. Congratulations, you've written your first program.
This time, change the print message to print My Bank Account Rocks!, and see if you can compile and run it successfully.
From time to time, we'll stop, write a Java program, and then learn some more.
While programming can be confusing, we just did a quick program to get your feet wet. Don't worry if you don't understand anything quite yet.
For now, be able to write the program above, almost exactly as is, but perhaps changing the print message, and we'll get to explaining what you've done so far.
Right now, you've done the rough equivalent of having someone tell you words to say in French to order some item on a menu. You didn't understand the words. You weren't sure what you're ordering (did you order snails?). You got some food. It's a little progress.
We'll now step back and start telling you about programming a little at a time. The stuff you've written will begin to become clearer and clearer, until you know what's going on.