Today we will be going over how to use variables in Java. Let's start a new class within Eclipse called Variables. Firstly, we must go over what exactly a variable is. For those of you who are math prodigies, math majors, or simply understand basic Algebra, a variable is a letter or group of letters that define something within your function. That's the math way of explaining what a variable is, and pretty much is a prime definition of what a variable is. Though in the programming world we take pride in using complex words to explain what a variable is relative to the machine.
In the programming world a variable is a named memory location capable of storing data of a specified type.
What that means is that a variable is something that stores data of a particular type. A type would be something like byte, short, int, and long. Which we will go into much detail sooner or later in this tutorial. But for the time being let's focus on expanding the definition of a variable.
In math we have, X=2, solve for x-5.
For the normal mathematician, this is quite simple x = 3 right? Well let's hope that you resolved the equation above to 3 since that's the correct answer. When the question says x=2, x is the variable and in the programming world the 2 is the initialized value of x.
An initialized value is when we give a variable a specific value. For example x=2.
When programming we must remember that: A variable must be declared before it can be used. For those of you who are wondering what that means, well we have to type out the variable at the beginning of our program (for the time being) before we use it.
An example of declaring a variable would be:
int x=2;
In this segment of code we declared the variable x which has a value of 2. We also gave x the type int which refers to integer. You don't always have to specify a value for x.
You can simply declare a variable by typing out:
int x;
Now let's go over what the basic types are:
byte: 8 bits (1 byte), which ranges from -128 to 127.
short: 16 bits(2 bytes), which ranges from -32,768 to 32,767.
int: 32 bits(4 bytes), which ranges from -2,147,483,468 to 2,147,483,467.
long: 64 bits(8 bytes), which ranges from -922,337,203,685,475,808 to 922,337,203,685,475,807.
We use numerous types for different reasons, but for numerical values you should memorize what these four types are and what their ranges are.
Let's write a simple program that uses variables shall we?
public static void main(String [] args){
int x = 2;
int y = 5;
int product = x*y;
System.out.println("The value of 2x5 is: " + sum);
}
Now as most of you guessed from that simply program we implemented three declared variables. Which we gave each variable a specific value. For x we gave it the value of 2 and for y we gave it the value of 5. Why did we do such a thing? Well in the print statement we said that the value of 2x5 is something right? So basically we used basic algebra skills to come up with the code. Which happens to be x*y. Since x has the value 2 and y has the value 5 we should get 10 as our sum. In my next post we will dive deeper into variables and their use, as well as, concatenation, and float and double types.
For today's assignment, your job is to write a program that multiplies 5x5. Send me your source code in the comments and if you have any questions feel free to ask.