First Steps in Java - Part 2
Variables, Data Types and Sizes
0: Intro | 1: Setup | 2: Variables, Data Types and Sizes | 3: Get Some Input| 4: Operators | 5: Strings | 6: Conditionals | 7: Loops | 8: Methods | 9: Arrays
Yesterday we created our first application and introduced the essential parts of a class, i.e. declarator and body. Today, we will take a closer look at variables and put them to use at the end of the lecture.
Identifier
Computer programs deal with data and data is stored in memory, i.e. a value is stored at a specific location in memory. This memory location has a unique address, which is expressed in a hexadecimal number. Since it would be inconvenient to refer to a storage location in memory by its hexadecimal number, we reference the particular storage location with an identifier. For example, instead of saying that the value '20' is stored at the memory location 0x0801, we can assign the identifier with the name age to the memory location and then write: age = 20. That makes it easier.
An identifier is a name that is given to a class, interface, method, or variable.
There are naming rules for creating an identifier. Java's identifier rules are:
- use: letters A-Z
- use: digits 0-9
- use: special characters % (percent) and _ (underscore)
- cannot use: spaces
- cannot start: with a number
Variables
A variable identifies a memory location that can hold data. Data can be of different types. Java is a strong-typed programming language. Every variable's data type needs to be declared.
Bit and Byte
Let's talk about the actual memory space that a variable needs. In Java, the smallest space that a variable can take is a byte, which is a group of eight bits. A bit (abbreviation for binary unit) is the smallest unit in computer theory needed. A bit can express a switch's on/off state, represented by the numbers 0 and 1. The binary system uses only 0s and 1s.
The smallest number of a byte is 0000 0000 and the largest 1111 1111, which is 255 in the decimal system. One way to get the result is by raising 2 by the power of 7). The ASCI II character set that we use for character representation of letters, digits, special characters, etc. on the internet, includes 128 different symbols. ASCI II requires a 7-bit storage capacity for one character, small enough for storage in a byte. To store one letter or one digit, we need one-byte storage capacity.
Data Types
As mentioned before, Java is a strong-typed programming language. Every variable must be declared with a data type and it cannot change after declaration. There are two groups of data types: primitives and non-primitives.
Primitives
Primitive data types are called by value. This means that the memory location stores the value of the identifier. The identifier calls a memory location in the stack (a type of memory) that stores the value, e.g. the identifier 'age' holds the value '20'.
Primitive data types start with a lowercase letter.
Numbers
Since numbers vary in length and type, i.e. integers and decimal numbers, Java offers a range of data sizes. The following list gives an overview of the data type's size in bits, bytes, and the maximal decimal numbers it can hold.
byte 8 bit 1 byte 2 exponent 7 = 255 (range -128 to 127)
short 16 bit 2 bytes 2 exponent 15 = 32,768
int 32 bit 4 bytes 2 exponent 31 = 2,147,483,648
long 64 bit 8 bytes 2 exponent 65 = 9.223372e+18
float 32 bit 4 bytes max. 6-7 decimal digits, end the value with the letter f
double 32 bit 8 bytes max. 15 decimal digits, end the value with the letter d
Text or Number
char 6-bit 1 byte 128; only ONE digit or letter; must be in single quotation marks
Boolean
boolean 1 byte false/true; default: false
Please note the special case of null, a value that does not refer to an object but to an uninitialized state. null has a data size of 2 bytes.
Non-Primitives
Non-Primitive data types are called by reference and not by value. They refer to the object and not the values that the object holds. The identifier calls a memory location that stores the location of the object, which is stored in the heap (a type of memory location).
Non-Primitive data types start with an uppercase letter.
Non-Primitive data types have the same size. They cannot be null.
Class
Interface
Array
List
String
After all this theory, let's get ready to code.
Variable Syntax
A variable declaration consists of the data type and the variable's name. A variable needs to be declared.
<variable type> <variable identifier>;
int age;
Initialize a variable by assigning a value:
<variable identifier> = <value>;
age = 54;
You can declare and initialize a variable at the same time: int age = 54;
Declare multiple variables on the same line: int age, height;
Declare and initialize multiple variables on the same line: int age = 54, height = 6;
Let's put everything in practice what we learned today. Create a file with the name DataTypes.java and create a variable for each primitive data type and a string variable. Either come up with your own variables or declare, initialize, and output variables for:
- the current world population
- the brand of your favorite snack food
- the price of your favorite food per unit
- your age
- the number Pi, as accurate as possible
- the single-letter amino-acid code abbreviation for Glutamine
- the zip code of your city
- your expected annual income for 2020
- does it currently rain?
Writing the entire file by yourself brings you the most benefits. The code for this lesson is available at GitHub.
This lecture is also available on Medium, First Steps in Java - Part 2.
Tomorrow we are going to learn how to get user input into your file.