-
Notifications
You must be signed in to change notification settings - Fork 0
Variables
A variable is a location in memory that that can hold data that may vary. A variable can be anything from a a number to a string of words.
An example of this can be seen in the Strings chapter where we made different literal strings. Numbers can also be variables. In fact, numbers are the main type of variables used when programming. This is especially true when you have a program that will do a lot of math.
A variable can hold many different values. To hold all these values a variable has many different data types. Below is a table of of the data types for a variable.
Data Type | Description |
---|---|
byte | Byte-length integer |
short | Short integer |
int | Integer |
long | long integer |
float | Single-precision floating point |
double | Double-precision floating point |
char | A single character |
boolean | A Boolean value(true or false) |
Declaring a variable is just as simple as declaring a String. All you do is you declare the data type, give it a name and if need be define a value for it. For example, let's say I wanted to declare an integer data type number. The syntax would be:
int numberOne = 23;
For the code above the data type is int, the name of the variable numberOne and the value of the variable is 23. Now that we have this variable declared we can use the variable name anytime to call the value of the variable. Now, what would we want to use this variable for? Pretty much anything really. If we want to use the variable for calculations then we could. If we wanted to output it to the user we can do that as well.
By "hard coding" I mean specifically writing out the value associated with the variable. For example let's say I have a program that uses the variable myNumber, which is equal to 2, in some way. Let's also say that I use this variable myNumber multiple times in different parts of the program. If down the line the program changes and I have to change the value of myNumber I only have to do it in one place, where I declared it. This is much better than have to go through the code line by line and change each individual place where I used the number 2.
You can also assign values to a variable after it has been created. To do this you just need to use the name of the variable a give it a new value.
//old value
int numberTwo = 45;
//code that uses the old value
//assigns a new value to the variable
numberTwo = 67;
A constant is simply a variable with a value that will not change. A constant is mainly used when you have a variable that you feel will never need to be changed in a program. For example, if I wanted to set a limit for a program I could use a constant because generally, a limit will never change. Declaring a constant is very similar to declaring a regular variable also. The only difference is that you use the final keyword. The final keyword does exactly what is says. It makes the variable final. It is also a good practice to declare constant variable names in all caps. This way other programmers can know that these are constants.
Example:
final int NUMBER_TWO = 30;