Java Datatypes and Variables
In this lesson you will learn what datatypes are, the relation between them & memory (bits & bytes), what variables are and how to declare & initialise them.
A variable is a container that holds values that are used, and reused, in your Java program. Every variable must be declared to use a specific datatype. Datatypes catagorise the type of information a variable can hold, this also determines how big the variable needs to be or how much memory it was be assignment.
Every variable in Java must have a datatype which defines the type and size of information stored in it. Each data type is tailored to a specific type of data required by the computer understand how that data can be handled. For example numbers can be used in mathematical statements and equation but words and characters cannot.
There are eight primitive data types in Java:
- byte, this type occupies 8 bits or 1 byte of memory
- short, 2 bytes
- int, 4 bytes
- long, 8 bytes
- float, 4 bytes
- double, 8 bytes
- char, 2 bytes
- boolean, 1 byte
Before we get into different datatypes, let’s get a handle on how much room these take up. At their most basic level, computers only work with 1s and 0s; therefore all data is stored as such. Bits, Nybbles and Bytes describe the basic building-blocks of data in a computer.
A Bit is the smallest unit of data in a computer and can only store a 1 or a 0.
By grouping 4 bits together to store a larger piece of data we create a Nybble. This is not a commonly used size but just know that is exists.
If we group 8 bits together (or 2 nybbles) to store an even bigger piece of data we create a Byte, and a single byte is enough to hold a single typed letter, e.g., ‘z’ or ‘A’. This is the most common form of memory expression, but you will occasionally see memory expressed in terms of bits too so understand the relationship between the two: 1 Byte = 8 Bits.
Remember that a bit is either a 1 or a 0, and that a byte (or 8 bits) is required to hold a single typed letter? You won’t be surprised then to hear that 01000001 is a bit representation of the letter A!
The number of possible patterns (or values) of 0’s and 1’s in a single byte is 256, therefore it can hold a number between 0-255. But how does a computer know that 01000001 is an A? This is where ASCII comes in. ASCII is a code for representing 128 English characters as numbers, with each letter assigned a number from 0 to 127. For example, in ASCII, number 65 means Latin capital ‘A’.
Let’s run through the most common datatypes you are likely to use.
char – Character Datatype
The char data type holds a single character. This is interpreted by the computer as a piece of ASCII code. Assumes a size of 2 bytes, but can hold only a single character because char stores Unicode character sets. It has a minimum value of 0 and a maximum value of 65,535. Unicode is a superset of ASCII, the numbers 0–128 have the same meaning.
int – Integer Data Type
The integer datatype holds a single whole number – with no fractional parts. It occupies 4 bytes of memory and has a minimum value of – 2,147,483,648 and a maximum value of 2,147,483,647. An integer is generally used as the default data type for integral values unless there is a concern about memory (Integral means consisting of a whole number or an undivided quantity).
double – Decimal point number
The double datatype holds a single decimal point number and occupies 8 bytes of memory. It can store values from 1.7e−308 to 1.7e+038 which is sufficient for storing 15 decimal digits.
float – Floating point decimal number
The float datatype holds a large decimal point number and occupies 4 bytes of memory. It can can store fractional numbers from 3.4e−038 to 3.4e+038, sufficient for storing 6 to 7 decimal digits.
boolean – true or false state
The Boolean datatype only has two states, true and false. It occupies 1 byte of memory since it only needs to store either true (1) or false (0) values. This is frequently used in branching code, loops etc.
String Data Type
The String Data Type is not a primitive datatype BUT it is used and handled like a primitive datatype and is actually more complex. The String data type is used to store a sequence of characters (text). When assigning a value to a variable using the String datatype, the values must be surrounded by double quotes.
Declaring a Variable
When you create a variable by giving it a name and a datatype, you are what ‘s called declaring that variable.
//Declare an integer variable called var1 int var1;
This variable is called var1, and is of an integer type (int). This variable can only hold a number that is an integer (a whole number with no fractional part). It also has a limit on its size – an integer variable is allocated 8 bytes of memory, enough space to store whole numbers from -2,147,483,648 to 2,147,483,647.
Java is a very strict language you must observe all syntax rules to avoid run-time errors with your code. Naming variables is no exception, the rules for choosing a variable name are:
- No spaces are allowed
- It must start with a letter, underscore or $
- The remainder of the name can include letters, numbers, underscores or $, but no spaces
- Java keywords cannot be used as variable names, you will know if you are using a keyword if it turns blue in you IDE)
When choosing a name for a variable consider what the variable will be used for and try to choose a name that would make it easier for you and others to quickly identify what the variable is used for. For example:
- num1 could mean any kind of number but studentAge suggests it is that age of a student
- text1 could be any piece of text but studentFirstName suggests that it is the first name of a student
Storing Values within Variables
When you declare a variable it is important to remember that it is empty until a value has been assigned to it. When you assign a value to a previously empty variable you are initializing the variable. For example:
//Declare an integer variable called var1 int var1;
Here, the variable var1 is declared with a name and datatype of int but is empty (note that this is a statement and must end in a semi-colon)
//Assign a value of 5 to a variable called var1 var1 = 5;
Here we are calling that variable, var1, and assigning a value it a that matches the variable’s datatype. At this point we have both declared and initialized the variable.
We can also declare and initialize a variable on the same line:
//Declare an integer variable called var2 and assign a value of 5 to it int var2 = 8; //We have declared and initialised the variable
Here we have declared a variable called var2 with datatype int, and assigned it a value of 8.
We cannot assign a value to a variable before declaring it:
//Assign a value of 2 to an as yet non-existing variable called var3 var3 = 2; //Declare the variable var3 int var3; //The assignment and declaration are in the wrong order and this code cannot work
Declaring and Initialising Variables
The following are examples of declaring and declaring/initialising variables of each of the main primitive datatypes and the String datatype:
char: When initialising a char variable, the value must be enclosed in single quotes.
char myLetter01; char myLetter02 = 'L';
int: When initialising an int variable, the value must be a whole number (no fractional part).
int myNumber01; int myNumber02 = 5;
double: When initialising a double variable, the value can include a decimal point.
double myDouble01; double myDouble02 = 10.2;
float: When initialising a float variable, the value can include a decimal point and must end with the letter f
float myFloat01; float myFloat02 = 15.2f;
boolean: When initialising a boolean variable, the value must be either true or false.
boolean myValue01; boolean myValue02 = true;
String: When initialising a string variable, the value must be enclosed within double quotes. The datatype, String, must begin with a capital S.
String myString01; String myString02 = "Learning Glue";
In this lesson you learnt what datatypes are, the relation between them & memory (bits & bytes), what variables are and how to declare & initialise them.