Hello! This tutorial will teach you about variables.
What are variables?
Variables are pieces of data which are used to store more data. Every variable has a type, a name and a value; if the variable does not have a value then its value is 'null' or 'nothing'.
What types can variables be?
There are a few different types of variable, the most common ones being:
- Int (Integer)
A string is a piece of text containing letters, numbers and symbols. An int (or integer) is a full number while a double is a number which can contain a decimal place. A boolean can be true or false and a long is the same as an integer but can hold more data in a wider range of numbers.
While having the common, pre-defined variable types, it is possible for you to make your own using OOP (Object Oriented Programming) which is very commonly used within Java.
When should I use variables?
You should use variables to hold data which should be kept for the current instance of the program while it is running. Normally the only data kept in variables will either be re-used multiple times within the program or it is holding a piece of data to break down the process of another script (for example a complicated maths sum).
You should NOT use variables to store data which you want to keep between program running times. This is because the data is stored temporarily within the computer systems RAM and is cleared once the program is shutdown.
How do I create a variable?
To create a variable you need to decide on a couple of things first; What type should your new variable have? What variable name do you want to give it? (The name is normally easily related to the value the variable holds, this is so you know where everything is stored). And finally you should decide what value(s) it should hold. Once you have the answer to all these questions you simply write them in order on a new line, like so:
The value of our new String variable (s) is encased with quote marks because anything between two quotes is made in to a string.
Once I have a variable, how do I use it?
Once a variable is created you can easily use it by using the name of the variable. For example; if we had a variable called "var" as a string and we wanted to output it in the console, we would simply write:
How do I use a variable?
Here is a quick example script I have used which declares one variable of each type from above and outputs them in the console line by line...
The output is:
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.