Variables
- A small piece of the computer’s memory that is used to store information.
- A variable has a type (for example number or character) and this determines
how much space the variable has in memory.
- A variable also has a name so that the programmer can refer to it.
- You can change the values of variables as a program runs.
Primitive Types:
Type – A kind of data that can be stored. There are two major kinds in java: Primitive
types and Objects.
| Type |
Contains |
Default |
Size |
| boolean |
True or False |
False |
1 bit |
| char |
One Character |
\u0000 |
16 bits |
| byte |
Integer |
0 |
8 bits |
| short |
Integer |
0 |
16 bits |
| int |
Integer |
0 |
32 bits |
| long |
Integer |
0 |
64 bits |
| float |
Decimal |
0.0 |
32 bits |
| double |
Decimal |
0.0 |
64 bits |
| String |
words |
null string |
- |
We will mostly use Boolean, char, int and double.
Declaring A Primitive Type:
int a;
char c;
string x;
Initializing A Primitive Type:
- Initializing means putting a starting value in, it is also a specific form
of an assignment statement.
int a = 0;
char c = ‘h’; //single quotes
for char
String x = “I love programming”;
//double quotes for string
Assignment Statements:
- Assignment Statements change the variable’s value
a = 9;
d = a+4;
x = IBIO.inputString (“Please
enter your name:”);