Kotlin
Java

Variables and Types
Java

Created By: Geoffrey Challen
/ Updated: 2021-08-23

Now let's begin talking about the basic building blocks of computer programs. We'll show how computers can store data using variables, and discuss how Java distinguishes between different types of data.

What Computers Are Good At

Computers represent the most powerful tool that humans have ever created. Part of what makes them so useful is that they are good at things that humans are not.

Over the next few lessons we'll be discussing several core computer capabilties:

  • Storing and manipulating data
  • Making simple decisions
  • Repeating the same steps over and over again

These abilities form the foundation of everything that computers can do. Even if computers sometimes seem complex—they are actually quite simple. (That doesn't mean that computer programming or computer science is simple or easy, far from it!)

Variables

Computers are great at storing information or data. Let's look at how we can store a single number in a Java program:

Run through each part of a variable declaration and initialization.

This code does three things:

  1. It tells the computer that we are going to store data in a variable named i
  2. It tells the computer that the variable i will store integer data (int)
  3. It sets the initial value of i to be zero

We have declared our first variable! As its name implies, a variable's value can and usually does change as the program runs.

Show how we can modify i later in the program.

Declaration and Initialization

Technically the statement int i = 0; is combining two things that we can do separately: variable declaration and initialization. Let's write it out on two separate lines:

While we can do it this way, Java will complain if we don't set an initial value:

Run the code above and see what happens. As a result, it's more common to see the variable declaration and initialization combined into a single statement.

Literals

In the code above we also used our first literal—the value 0 that we used to initialize i. A literal is a value that appears directly in your code. Unlike a variable, it's value does not change.

Note that in Java, character literals must be enclosed in single (not double) quotes. So this won't work:

Types

All data in Java is represented by combinations of 8 different types of value. These are known as the Java primitive types.

Here are all 8 Java primitive types, broken into four categories:

  1. Integer values: byte, short, int, and long
  2. Floating point values: float and double
  3. Boolean values: boolean
  4. Characters: char

Integer Values

Java has four different primitive types for representing integer values. An integer is a number without a decimal point. So 0, 8, 16, and 333 are all integers, but 8.7, 9.001, and 0.01 are not.

Why does Java have four different types for representing the same kind of data? Because each can hold a different range of values. byte variables can store values from -128 to 127, while int variables can store values from -2,147,483,648 to 2,147,483,647: But int variables also take up more computer memory. Don't worry too much about these distinctions now. We'll almost always use int variables to store integers in this class.

Floating Point Values

Java provides two types for storing decimal values: float and double. Similar to with integer values, float variables can store a smaller range of values than double variables. We'll commonly use a double when we need to store a floating point value.

Boolean (or Truth) Values

In order to make decisions about what to do in our programs, we'll frequently want to determine whether something is true or false. Variables with Java's boolean type can store only two values: true and false.

Character Values

Finally, Java provides the char type for storing a single character value.

Why Types?

Use some analogy to explain why types are important in a computer program. Examples: flexible v. fitted container. One can store more objects, but you never know what's in it.

Not every programming language requires that you indicate what type of data a variable will hold. For example, this is valid code in the Python programming language:

So why does Java require that we indicate what type of data each variable holds? Because it allows the computer to help you write correct programs and avoid errors. By keeping track of what type of data a variable holds, Java can help us make sure that certain operations on that variable are valid. This will make more sense as we go, but we can already see these checks at work in a simple case:

Demonstrate what happens if we try and assign a floating point value to i.

Representation in the Digital Age

Examining the primitive types, you'll notice that 6—byte, short, int, long, float, and double—are explicitly for storing numbers. What about boolean? Well, we represent false as 0 and true as 1.

That leaves char as the outlier that doesn't seem to store a numeric value. But... it does! Here's how:

What is shown above is the mapping from numbers to character values. You can see that in action as you use the char type in Java:

The mapping above wasn't sent down on stone tablets. It was agreed upon at the dawn of the computer age. (And it leaves a lot to be desired, since there are many symbols in many alphabets that are not included!)

But it makes an important point. Internally, computers store everything as a number. Any non-numeric data must be converted to numeric form—or digitized—before it can be manipulated by a computer.

We live in the digital age. You enjoy music delivered in a digital format and take photos with a digital camera. You can increasingly enjoy fine art from a distance due to high-resolution scans. One day soon you'll have a completely digital medical record, consisting of medical information that was itself digitized so that it could be analyzed by a computer. Learning about computer science and programming will allow you to be a full participant in our digital present and future.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

AI4All

Interested in AI? And helping people? Then AI4All may interest you!