1️⃣

3.1 Primitive Types

Chapter Table of Contents

💾
Ch. 3 Data

Section Table of Contents

image

Primitive data types are the most basic types of data. They are stored by value, which means that when you declare a variable of a primitive type, the value of that variable is stored at that place.

Non-primitives (aka objects, which we will learn more about in Ch. 12), on the other hand, are more complex types of data that often build off of primitives. For example, a String is a non-primitive, which is made up of multiple characters (char), which is a primitive. Unlike primitives, non-primitives are stored by reference, which means that when you declare a non-primitive variable, it points to another location in memory which stores the value of the variable.

Here is a diagram that might help you visualize how primitives and non-primitives are stored differently:

image

The variable age is a primitive with the value 5, which is stored by value. The variable name is a non-primitive, which is stored by reference, where the variable points to a location in memory where the value is stored.

For now, you won’t need to worry too much about the difference between store by value and by reference (just keep it in mind). The important part is that you recognize that there are 2 types of data: primitives and non-primitives.

Below are a few commonly used primitive types.

Boolean

Booleans can store 2 values: true or false. This is because when you wind it all down, a computer is built from binary code (1s and 0s, on or off, true or false). These variables, along with logical operators (see Ch. 5), are what are going to allow you to make decisions in your programs.

boolean isHungry = true;

Notice that when you’re naming booleans, you often use the format verbAdjective, such as isHungry or isTired. This is a convention that helps with readability when dealing with loops and selection structures, which we will learn about later.

Char

This is a character. Sometimes when you only have one character there's no point of putting it into a String, you can just use character.

The way it's declared and initialized:

char letter = 'h';
💡

Notice that there are single quotes instead of double quotes.

Byte

The data type which takes up the least amount of memory and stores integers. In fact, it takes up 8 bits (or 1 byte, hence the name).*

Minimum value is -128

Maximum value is 127

Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.

💡

A bit is something that stores either a 1 or 0. A byte, therefore, is a series of eight 1s or 0s.

Short

Short data types are also like ints (see below) but use less memory.

Minimum value is -32,768

Maximum value is 32,767

Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer, and 2 times a byte data type.

Int

This data type stores whole numbers, negative and positive, (integers). It's 32 bit. They range from:

Minimum value: -2,147,483,648

Maximum value: 2,147,483,647

The way to declare and initialize:

int age = 5;

Notice that if you give it a decimal value it will give you an error.

For example:

int age = 5.5; // error
🚨

int is not a good idea to use in calculations where you want to be precise.

Long

Long data types can hold long pieces of data! When you use this data type you are also taking up a lot of memory. In fact it's a 64 bit, so it uses twice as much memory than an int.

Minimum value: -9,223,372,036,854,775,808

Maximum value: 9,223,372,036,854,775,807

Declaration and initialization:

long a = 567894567L;
⚠️

Notice that there is a capital letter L after the number. This is to tell Java that it is a long, because by default Java assumes it is an int.

For certain numbers in Java (including longs), you can add underscores where commas would normally go without having a syntax error. For example, the code below does the same thing as the code above, except it’s easier to read:

long a = 567_894_567L;

Float

Float stores numbers with decimals. Float is mainly used to save memory in large arrays of floating point numbers. It's 32 bit and closely related to doubles.

Declaration and initialization:

float b = 567.8f;
⚠️

Again, note that there is a lowercase f (you can also use an uppercase F) after the number to tell Java that it is a float. This is because by default, it assumes that any number with decimals is a double.

Double

This data type stores variables with decimal points, but is more precise than float. It is 64 bit, which is double the size of a float. The way they are declared and initialized:

double c = 5.67;

If you store an integer in a double it will convert it automatically. For example:

double d = 5;
System.out.println(d); // prints 5.0

If you want to learn more about binary in Java, you can read this article. If you want to learn more about primitives, you can read this article.

Practice

Initialize

Let's practice initializing different primitive types. In the code provided (see template), there are variables initialized, but they don't have a type! Based on the name of the variable and its value, write the correct type in front of the variable's name so that the program works. (Be sure to uncomment everything when you begin working.) Note: There may be more than 1 correct answer.

Next Section

2️⃣
3.2 Non-Primitive Data Types

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.