Wednesday, December 15, 2010

Tutorial 1: The Basics

This is the first tutorial in this series so I'm going to go over the bare minimum requirements to start programming in Java. I'm going to cover statements, expressions, variables, and give you some sample programs so you can see what they look like.

Every single task in Java can be broken down into statements. A statement is a command that causes something to happen and are very simple to understand. Here are some examples of statements:

  • int count = 445;
  • person.name = "Alice";
  • System.out.println("Hello, this is a program.");
  • int aNumber = 5 * 2
The last statement in the list is called an expression. An expression is just a statement that produces a value. When I multiply 5 and 2 I have produced the value 10.

A statement in java is always terminated with a semicolon (;) at the end. You can have as many statements on a line as you wish, but each must have the semicolon in the correct spot.
paul.height = 6; paul.eyeColor = Color.RED;

Next I'm going to talk about some variables and data types. A variable is just a place where info can be stored during a program's runtime. The information can be manipulated anytime during the program. There are three kinds of variables that you will use in Java:

  • Instance Variables - used to define an object's attributes (any information that object might need.)
  • Class Variables - used to define attributes for an entire class and apply to all instances of that class.
  • Local Variables - are used inside blocks of statements and only exist within those blocks. After the block is executed the variable does not exist.
Here is an example of a class that uses instance variables:

public class InstanceVar {
     private int myNumber;

     public static void main(String[] args) {
          InstanceVar myInstance = new InstanceVar(10);
          System.out.println("Done!");
     }

     public InstanceVar(int number) {
          myNumber = number;
     }
}

This program doesn't actually do anything, but you can see that the class InstanceVar has a private instance variable named myNumber. This variable is set in the initialization of the object. When the line:
InstanceVar myInstance = new InstanceVar(10);
is called then the variable myNumber is set to 10.

The next example is going to be an example of using a class variable:

public class ClassVar {
     public static int myClassVar = 50;

     public static void main(String[] args) {
          ClassVar.myClassVar = 100;
          System.out.println(ClassVar.myClassVar);
     }
}

This program will have the output of "100." In the class ClassVar the first line:
public static int myClassVar = 50;
creates a class variable (by using the static keyword) that is public and has a value of 50. When the line:
ClassVar.myClassVar = 100;
is called, the value of ClassVar's myClassVar variable is set to 100. The line with the call to System.out.println() prints out the value of myClassVar.

Here is an example of a program that uses a local variable.

public class LocalVar {

     public static void main(String[] args) {
          int localVar = 20;
          System.out.println(localVar);
          localVar = 1000;
          System.out.println(localVar);
     }
}

This program creates a local variable named localVar that is initially set to 20. The next line prints out the current value of localVar. Then we change the value of localVar in the next line to 1000. Calling System.out.println(localVar); a second time will print out the updated value.

So I hope you understood most of what I was talking about. This first tutorial wasn't supposed to get you programming yet, but was to make you somewhat familiar with what I will be talking about in tutorials to come. I'm thinking that the next tutorial will be on naming variables, primitive data types, and arithmetic.

1 comment:

  1. hey nice source for us, thanks for sharing this details and i bookmark this information for future use.

    Java Development Company

    ReplyDelete