Dec 20, 2007

Would You Like Some Variables As Well?

On a quick note beforehand... how did you find the looping post? I may end up redoing it for you guys if you didn't "grok in fullness" because I may not have done it justice, the <> statements blogger really doesn't like and I had to rewrite the whole thing a couple times. Am searching for another method to show proper form currently (but for now examples won't be tabbed in).

Alright so we have used variables already but they weren't really explained in any sort of detail. Variables are a sort of container but they are picky containers. Tupperware with attitude. If you try to put something in it it doesn't like it spits it back out with a nasty error message. To declare a variable you must say what type it is and give it a name. To use a variable it must be initialized. So here is an example of a statement declaring a variable:

int x = 0;

The int is the type of variable x is.
x is the name of our variable.
= 0 initializes our variable.

We can also declare a variable and initialize it in two steps:

int x;
x = 0;

Now for our purposes at the moment our variables are going to go inside our main() method. This is for reasons that will be explained in upcoming lessons.

Now you aren't just going to want to use integers. You are going to want to use many different types of variables. Here is a list of the variables for you:

int, these are integers and can store positive and negative nondecimal numbers.
boolean, these are true and false operators and must be set to true or false.
byte, can hold values from -127 to 128 according to this site.
double, can hold decimal values.
long, can hold really long nondecimal numbers, kind of like a super integer.
char, can store characters like "j" or "a".
String, can hold a bunch of characters strung together, like "hello there" or "ass22d&&^(*".

There we are, toy with those at will, we'll get them out of the main() method next time.