Variables

Submitted by: 
Language: 

Introduction:
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:
- String
- Double
- Int (Integer)
- Long
- Boolean
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:

  1. String s = "Hey012';[";

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:

  1. System.out.println(var);

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...

  1. String s = "Hey012';[";
  2. boolean b = true;
  3. int i = 100;
  4. double d = 100.203;
  5. long l = 999999999;
  6. System.out.println(s);
  7. System.out.println(b);
  8. System.out.println(i);
  9. System.out.println(d);
  10. System.out.println(l);

The output is:

  1. Hey012';[
  2. true
  3. 100
  4. 100.203
  5. 999999999

Finished!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.