Introduction to programming, Lesson 1


Alright, now that you have a general understanding of how computers work, lets talk about programming. Programming can be very tedious. We've been "programmed" (forgive the pun) to believe that computers are smart, since they can do many things today that humans can't do. Actually, computers are the stupidest things we regularly interact with. When programming, you must tell the computer exactly how you want it to behave, otherwise, it will misbehave, or not work at all. It can be frustrating at times.

Programming in Java consists of three steps:

  1. Writing source code
  2. Compiling source code into byte code
  3. Using the Java Virtual Machine to execute the byte code
Listed below is an example of the simplest Java source code that you can write:

  public class HelloQuince
  {
      public static void main(String [] args)
      {
          System.out.println("Hello Quince!");
      }
  }

Notice that it is formatted neatly and each line is clearly indented from the other lines. While the computer does not care about neatness and indentation, humans do. In fact, half of being a 'good' programmer involves writing code that is easily read by other humans. After all, if another human cannot look at your code and figure out what it is doing (or at least follow the program flow), you've done no one any favors. Commonly you'll revisit code you've written months or even years before. If you haven't written the code cleanly, you'll have a problem understanding your own code!

Now lets talk about each line of code individually. Here's the code again, numbered line by line:

  1 public class HelloQuince
  2 {
  3     public static void main(String [] args)
  4     {
  5         System.out.println("Hello Quince!");
  6     }
  7 }
Line 1 public class HelloQuince indicates that you are defining a new class called "HelloQuince" and the following code belongs to that class. A class is a block of code structured in a way such that all its parts (or "methods", see below) can work together to perform a task. In this case, the class is known to the computer by the name "HelloQuince". From now on, when you type HelloQuince into the source code, the computer will know you are referring to this class. The word "public" on line 1 is there (as opposed to the keyword "private") to indicate that this class can be used by other classes that you may write (classes commonly work together). A private class cannot be accessed by any other classes, the importance of which you'll understand later on.

Line 2, the opening curly brace is there to indicate the start of a block of code. This particular block of code starts on line 2 and ends on line 7, as indicated by the closing curly brace. This block holds the code that describes the class "HelloQuince".

Line 3 public static void main(String[] args) indicates the start of a "method". A method is intended to be an operation that does a single task. Again, the word public indicates the method is usable by other classes. If the method was declared to be a private method, no other class could use the method. (Don't worry too much about public and private just yet.) The keywords "static void" are inconsequential for now and are not necessary to know in order to understand this basic code. The keyword main, however, is very important. "main" is the name of this particular method. You may have as many methods as you require in a single class, and the name is how you (and the computer) distinguish the methods from one another. Additionally, "main" is a special method indicating the start of the program. Generally, all programs start execution at this point, not at the top, as you might think. The words in between the parentheses indicate the method arguments. Remember the algebra statement f(x) = 2x + 4? You know that the value of 2x + 4 depends on the value of x. Thus, in programming terms, the value x is the argument to the function 2x + 4. Likewise, String [] args is the argument to the method "main". That's all you need to know about arguments for now.

Line 4, another opening curly brace, is the start of another block of code. As indicated, this particular block of code starts at line 4 and ends at line 6. Lines 4 through 6 indicate the method body for the "main" method. Blocks of code can be "nested" (one inside the other) as you can see in this example.

Line 5 is where all the action takes place. System.out.println("Hello Quince!"); will print the words "Hello Quince!" onto the screen. This is the one and only line of code inside the main method, although there can be as many as are required. Note that "System" is separate class! The System class has already been written for you by the authors of Java. It is a group of related methods that takes care of all the low-level details of the hardware and software so that you don't have to. out refers to yet another class that concerns itself with writing text to the screen. (If the "out" class was not available, simply writing text to the screen would be a LOT more complicated"). "println" is a method. More specifically, it is a method of the class "out" in the same way that "main" is a method of the class "HelloQuince". We know something else about "println". We know that it must be a "public" method, since we are using it from the class "HelloQuince". Remember, private methods can only be used by the class to which they belong, but public methods can be used by any class. (If you try to use a private method from another class, you'll get an error. Again, you'll learn more about the importance of this later.) The text "Hello Quince!" inside the parentheses of line 5 is the argument to the method "println". The argument tells the println method what you want to write on the screen. The semi-colon indicates the end of the line, and is required after every line of code. In this case, after the computer executes line 5, there is nothing else to do. The program is done and it exits.

Lines 6 and 7 are just closing curly brackets to indicate the end of the two blocks of code.


Whew! That was a lot to absorb! I'm sure you'll have questions. Don't worry if you don't understand everything I've related to you. You'll understand the details as you go on. The important things to remember from this lesson is the concept of a method (a group of related instructions that perform a specific task) and a class (a group of related methods that typically perform a larger, more general task). If you'd like to compile and run the program we've written here, follow these instructions:

  1. Open up a text editor such as Notepad.
  2. Copy-paste the source code from the top of the screen (the one without line numbers) into the blank Notepad document.
  3. Save the file. Use "HelloQuince.java" as the filename.
  4. Open up the command prompt. Start > Run > type in "cmd" (no quotations), press enter.
  5. The command window comes up. Navigate to where your file was saved. For Quince, you should type "cd Desktop" if you saved it to the Desktop.
  6. Type "javac HelloQuince.java". This will compile the program.
  7. Type "java HelloQuince". This will run the program.
Did it work? Let me know.
Hosted by www.Geocities.ws

1