First Steps in Java — Part 2

mike dietz
5 min readMay 6, 2020

--

Variables, Data Types, and Sizes

1: Setup | 2: Variables, Data Types and Sizes | 3: Get Some Input | 4: Operators | 5: Strings | 6: Conditionals | 7: Loops | 8: Methods | 9: Arrays

Photo by Sander Dalhuisen on Unsplash

You are exactly my type! Do you want a bit of my byte?

Yesterday, we created our first Java application. Today we are going to take a look at variables, one of the building blocks of a Java class.

Identifier

Computer programs deal with data, may it be in your calculator, in your IoT devices, in the web form application that you submit or the game that you play on your phone.

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 is inconvenient having 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 age to the memory location. Now, we can 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)
  • do not use spaces
  • do not start with a number

Variables

A variable identifies a memory location that can hold data. Data can be of different types. Since 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. What is a byte, you may ask? It is a group of eight bits. And what is a bit? Well, a bit is an abbreviation for binary units and it 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.

A byte is a group of 8 bits. The smallest number is -0b10000000 (-128 in decimal system), and the largest number is 0b0111 1111 (127 in the decimal system). The ASCI II character set that we use for character representation, e.g. letters, digits, special characters, etc., includes 128 different symbols. ASCI II requires a 7-bit storage capacity for one character, small enough for storage in a byte.

Data Types

As mentioned before, Java is a strong-typed programming language. Every variable must be declared with a data type and, once declared, it cannot change. That means that Java is not only strong-typed but also static-typed. The two groups of data types are primitives and non-primitives, and we are going to take a look at them now.

Primitives

Primitive data types are called by value. This means that the memory location stores the value that the identifier points to. The identifier points to a memory location in the stack that stores the value, e.g. the identifier ‘age’ points to a memory location that 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 in decimals.

byte | 8 bit | 1 byte | 2 exponent 7 = 255

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, end with the letter L

float | 32 bit | 4 bytes | max. 6–7 decimal digits, end with the letter f

double | 32 bit | 8 bytes | max. 15 decimal digits, end with the letter d

Text or Number

char | 6-bit | 1 byte | 128; only ONE character; use 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.

Non-Primitive data types start with an uppercase letter.

Non-Primitive data types have all 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 also declare and initialize a variable at the same time:
int age = 54;

It is permitted to 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 Variables.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 estimated annual expenses for food
  • does it currently rain?

It benefits you the most if you write the entire file by yourself. Check yesterday’s lesson if you don’t remember the main() method or the SOP code.

Get the code for this lesson at GitHub.

Tomorrow we are going to learn how to get user input into your file.

--

--