Master Java Quickly Part 3: Data Types and Variables

This article will give you a solid understanding of data types and variables, along with how to build an application to display them.

This section explores the most fundamental elements of any programming language. Programming languages have different types of data you can store, known as data types. Java offers eight primitive types of data that we will cover. Data is stored inside of a piece of memory called a variable. A variable is a container that holds data that can be used and manipulated within a program. Variables allow you to store values, such as numbers or text, and reference them later in your code.

Primitive Data Types

Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. These are considered primitive because they are the most basic data types available. Computers can only understand binary which consists of bits that contain two values a 1 or a 0. Humans most often use a decimal system for representing numbers, which gives you values between 0 and 9. When you write code, you will be using decimal values for numbers, but your computer will convert and read them as binary. Let delve into each data type:

  • byte: A byte is the smallest unit of space you can store whole number values (integers) in. It consists of 8 bits (1’s or 0’s), which allows a combination of 2^8 or 256 values. These values can be positive or negative, giving a range of -128 to 127 numbers.
  • short: A short also contains whole number values, but occupies a smaller memory address of 16 bits (or 2 bytes). A byte is just 8 consecutive bits. Having a smaller amount of space to store your mean you can store values ranging from -32,768 to 32,767.
  • int: This stands for integer, which is just a whole number value. A location in memory generally consists of 32 bits (1’s or 0’s). This means there are limits on how much data can be stored in an Integer. The range of values can be from -2,147,483,648 to 2,147,483,647.
  • long: A long offers twice as much space as an integer. It gives you 64 bits of memory (8 bytes) allowing you to have an even greater range of values. This gives between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 different numbers that can be stored.
  • float: Many programs require more precise values of numbers which consist of fractions or decimal values. A float has a memory space of 32 bits, which allows you to store 6 to 7 decimal places. (Example: 5.334534)
  • double: If your application requires even more precision, you can use a double which gives you 64 bits of memory. This means you can store up to around 15 decimals points. (Example: 0.44223434323432)
  • char: A char stands for character, which is just a single letter. Programs need to display text and all text is made up of characters. Keep in mind computers can only process numbers, so characters are interpreted as numbers from the perspective of a machine. A char gives 16 bits of memory space, giving 0 to 65,536 values. There is no such thing as a negative char. Each of these number values is mapped to a corresponding character. Characters can be anything from lowercase and uppercase letters, to symbols such as & or $.
  • boolean: Many situations require just true and false values. True and false are just represented as a bit of 0 and 1, with 0 standing for false and 1 representing true. Therefor you only need 1 bit of space to store a boolean.

Variables

A variable is simply a container that is used to hold data types. In Java we use = to assign some value variable. You can declare a variable in Java, meaning you are just giving a readable name to a chunk of memory, but are not actually assigning any data. The data can then be assigned to that variable later on. Most of the time you will declare and assign a value to a variable in one statement. Variables in Java are written in camel case, meaning the first word in a variable name is lowercase and all the subsequent words are uppercase. Lets explore some examples.

        int declareNumber;                 // Declare an integer
        declareNumber = 42;                // Assign a value to that integer.
        int declareAndAssignNumber = 42;   // Declare and Assign an Integer value.
        float height = 5.9f;               // Floating-point number
        double price = 19.99;              // Double-precision floating-point number
        char letter = 'A';                 // Character
        boolean isJavaFun = true;          // Boolean
        byte smallNumber = 10;             // Byte
        short shortNumber = 5000;          // Short integer
        long largeNumber = 100000L;        // Long integer

Project Example

Lets build a simple application that just declares and assigns the variables listed above and displays them to our screen. We will be using IntelliJ IDEA as our IDE for setting up the project and gradle as the tool for building and running the application.

Open IntelliJ IDEA

Launch IntelliJ IDEA from your desktop or start menu.

Create a New Project:

From the top left hand corner of your screen, select File -> New -> Project....

  • Choose a name for your project. We will name it DataTypesAndVariables.
  • Select a location for your project files. We will be using ~\Documents\Projects\Tutorials.
  • The language will be Java and our build system will be Gradle.
  • Make sure to select your JDK version that was installed in the previous tutorial. We will be using OpenJDK 21.0.1 for this application.
  • We will use Kotlin as our language in Gradle. (Don’t worry about this for now)
  • There is an option to add sample code to your application. Lets use this feature to save time!

Write the Code

Lets create our data types and variables and display them to the screen. Between the curly braces next to your main() function, enter the following code (typing it our will help your comprehension). All this program does is assign values to each of the different data types and then uses the println method to display those values to the screen.

    public static void main(String[] args) {
        int declareNumber;                 // Declare an integer
        declareNumber = 42;                // Assign a value to that integer.
        int declareAndAssignNumber = 42;   // Declare and Assign an Integer value.
        float height = 5.9f;               // Floating-point number
        double price = 19.99;              // Double-precision floating-point number
        char letter = 'A';                 // Character
        boolean isJavaFun = true;          // Boolean
        byte smallNumber = 10;             // Byte
        short shortNumber = 5000;          // Short integer
        long largeNumber = 100000L;        // Long integer
        System.out.println(declareNumber);
        System.out.println(declareAndAssignNumber);
        System.out.println(height);
        System.out.println(price);
        System.out.println(letter);
        System.out.println(isJavaFun);
        System.out.println(smallNumber);
        System.out.println(shortNumber);
        System.out.println(largeNumber);
    }

Run your Program

Running your application is as simple as selecting the play button at the top of your screen.

After your program runs you can navigate down to the bottom of IntelliJ IDEA and scroll up just a bit. Inside of the terminal you should see each of your variables displayed on a new line.

Conclusion

There you have it! You should now have a solid understanding of how to declare, assign, and display different variables in a Java application. Variables and data types are foundational concepts in Java programming, allowing you to store, manipulate, and utilize data effectively within your programs. Variables act as containers for storing data, and their types define the nature of the data they hold, such as numbers, characters, or true/false values.

Index