Variables and Literals
Variables are dynamic and can be changed. Variables basically hold data or information. Think of them as slots in a memory bank. Variables can be assigned values easily and they hold that data until they are changed or discarded as redundant.
A literal is a fixed type of data (the types of data that can be stored in a variable). There are four types of literals. These are:
- Booleans (True or false)
- Characters and Strings (character is a single letter, Strings consist of usually more than one character)
- Numbers (Java supports integer, floating point, byte literals)
- Nulls (The Null literal is literally what it means...nothing)
Examples of all above:
boolean hairywoman = true;
char initial = p;
String surname = "rabbids";
int integer = 3;
float floating = 3.1467788557757785464434 ; (floats can hold 32 bits of data)
double mega_floating = 3.4e+038; (doubles can contain up to 64 bits of data)
byte byte_literal = 2; (nobody really uses bytes anymore as integers do the same job)
When we are coding we will often need to hold information that a method or part of the program will require to use later. Variables do this efficiently. You can use them in multiple ways to achieve a goal for example.....
int a = 5;
int b = 3;
int result = a + b;
the result variable now holds an integer 8.... clever stuff!
you could then use a boolean in a loop (we will get to loops later)
if (result == 8 )
{
boolean answer = true;
}
else
{
boolean answer = false;
}
Thats all there is to it folks!
No comments:
Post a Comment