Wednesday, August 11, 2010

3. Variables

So this tutorial will just educate you about some variables!  Ready?

Here is a variable that you used in the very first tutorial.  It's called a String and what it is is a string of characters.  For example "Hello World" is H-e-l-l-o- -W-o-r-l-d put together!

To create a string you just do:
String x = "Hello World";
String auwbrub = "alkjbal urnileiu";
This would create a String called x that contains Hello World.  
You can call a string (almost) whatever you want and you can put (almost) anything in it as long as it has the quotation marks around it.

The next is an int which you worked with in the second tutorial.  The name int is short for integer which is any non-decimal number.
To create an in you just do:
int x = 6;
int aljbwiubr = 1234;
Just like a String (and most other variables) you can call it (almost) whatever you want and put (almost) whatever in it!  (I won't be repeating this anymore but just know that it applies to all the variables I'll be teaching you about today!!!!!!!!!!)

Variable number 3 is the char.  Char is short for character and it is a variable that contains one character.  So a char could be H or E or L and a string is a bunch of chars put together.  (A char can only contain one character so don't trying putting more that one character in one)

To create a char you just do:
char x = 'H';
char alvjn = 'a';
Notice that the char only uses ' whereas a String uses ".  This is very important so don't try to create a char like this:
char a = "t";
It won't work!

The next (and last) variable (for this tutorial) is the float.  A float is an int but you can have decimals.  An int can only contain a whole number like -9 or 4 but a float can have 6.9248 or -372.89.

To create a float you just do:
float x = 12.34f;
float alburw= 86.3f;
The float is good for precise measurements and blah.  The int is great for simple calculations and simple adding but when you can't use it you need the float.  Now that funny little at the end of the numbers tells the system that is is a float.  There is another variable called the double which I'll tell you about in a second but it is not the same as the float so the system needs a way to tell the difference between a float and a double so you have to add an f to tell the system that it's a float.
(The double is just like the float but can contain larger numbers than the float.  In java variables can only contain as much as they are designed to contain.  If a float can't hold a high enough number then you need to switch to a double.
To create a double you just do:
double x = 12.34;
double agbgleu = 86.3;
Okay so I snuck in another variable but hey, you came here to learn right?)

So now let's combine all that we've learned.  Create a new class called variables and put this code in there:

public class variables {
    public static void main(String[] args)
    {
        String a = "I'm awesome";
        int b = 123;
        char c = 'c';
        float d = 12.34f;
        double e = 12.34;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
    }
}


This shouldn't be anything new to you since we just went over all of it the only thing you need to know is how to call a variable.  To do it you just use it's name like in String a = "I'm awesome"; you can call it later by saying System.out.println(a); so you can store information and call it later.  Just DON'T HAVE TO VARIABLES WITH THE SAME NAME!!!!!  It will cause major problems and could really mess up your program so just don't do it!  If you have any problems just drop a comment.  Thanks!
  

No comments:

Post a Comment