PT - Variables and Data Types (Lesson)

APCompSci_LessonTopBanner.png

Variables and Data Types

Introduction

Numbers, words, pictures…all forms of data are around us all the time. Think of your favorite apps that you use on your phone or computer. Does it store pictures? Share comments through social media? Is it a logic game dealing with numbers? Or a word game you play with friends? Every computer program deals with data. In its simplest form, Java data types can be broken into two categories: primitive and objects. A data type is a set of values and operations that can be done with them. In this course, the three primitive data types that we will learn about are int, double, and boolean. You will have the opportunity to explore many more data types, however, we will stick to those three primitive types for this course.

Data Types

We have gotten to the point where we need to start speaking a new language. When we want to give the computer instructions, we cannot just speak to it and expect it to respond. We must translate our words into a computer language (Java). The first things we will translate are data values.

There are three primitive data types that we will use in this course. They are int, double, and boolean. (Notice the way they are typed – all lowercase. Java is case sensitive, and these are keywords so they must be written exactly as you see them.)

int and double are used to represent numerical data. int represents integers and double represents decimals.

For example, you may use an int to represent your age or house address. A double can be used to measure the amount of money in your bank account or your GPA.

The boolean data type holds a true or false value.

One more data type

There are other data types which will not be tested on the AP Exam. Even though they won’t be on the AP exam, we may use them in this course just because it makes things more interesting.

Here is one:

char

Integers and decimals can be stored as int and double but what about if we want to use a single letter or character? Data type, char, stores letters. For example, you might store your middle initial as a char. The char data type can be used to store answers to a multiple-choice question.

Data Types and Variables

Data that is used in a program will be stored in variables. (Just like in math! Remember x?) Each variable that we create has associated memory in the computer that is used to hold its value. The memory in the computer that is associated with a variable of a primitive type holds an actual primitive value.

In math, variables, like x, can change – that’s why we call them variables. That can happen in programs as well. It is possible for a variable to be declared as final (Java keyword). That means its value cannot be changed. Typically, though, variables are place holders in code to represent any value that can be entered by a user or another program.

Try typing these values into the interactions pane of Dr. Java.

(That cool tab at the bottom when you open Dr. Java.)

Interactions Pane Console Compiler Output
Welcome to Dr Java int page = 9;
double price = 3.08;
boolean weekend; 

Then, type just the variable name, hitting enter after each one and see what happens. Notice that weekend has not been initialized. What value is stored in weekend by default?

Variables & Data Types Flashcard Activity

Please complete the flashcard activity below. Click "Continue" below to begin.

 

Reference Variables

A reference variable refers to a location in memory that stores an object.

  • Snowman bob = new Snowman();
  • Snowman fred = new Snowman();

In the above statements, Snowman is a class (notice that it is capitalized). The variables bob and fred are references to Snowman objects. Think of them as variables like x and y in math. They are simply names that refer to data. (new is a keyword in Java. It refers to a new Snowman object. More about that later…)

The first part of each statement, Snowman, declares a reference variable to hold a Snowman object. The second part of each statement, = new Snowman(), creates a Snowman object and assigns the variable (fred/bob) to the location where it is in memory. (Also more on that later…)

Remember, a variable is a storage location for some type of value.

Variable Names

Sometimes primitive variable names are referred to as identifiers. Think of an identifier as your name. It is a way to identify yourself. Variables are the same. If I have two integer variables, I will want to have two different names to tell them apart.

There are a few rules to follow when naming variables. (And we will follow them!)

  1. First, you should choose meaningful names. When you or someone else reads your code, it should be clear what each variable represents. For example, if you have a variable that represents length then use length as the identifier. Identifiers that do not make sense or match the purpose of the variables can be very confusing and even cause you to make mistakes in more complicated programs.
  2. Second, all identifiers should start with a lowercase letter. They can contain letters, numbers, and underscores. No other characters allowed. (The code will compile when it is capitalized but remember we save capital words for class names.) Side note: Java is case sensitive which gives even more options for variable names. For example, Edwin is not the same as edwin.
  3. Third, if an identifier is more than one word long, capitalize the start of each word except the first. This is called camelCase, for example, daysOfTheWeek.
  4. Fourth, you cannot use any of the Java keywords as variable names. Some of the keywords that we have studied so far are: int, double, void, main, public, static, new, and class. As you work in Dr. Java, you will notice the font color will automatically change when a keyword is typed. Use that as a reminder when you are coding.

BookIcon.png Click on "Runestone Academy" below to open the required reading listed.

Runestone Academy Links to an external site.: AP CSA – Java Review:

READ:

  • 3.1 – What is a Variable?
  • 3.2 – Declaring Variables in Java
  • 3.3 – Changing Variables in Java
  • 3.4 – Naming Variables

BookIcon.png Click on "Introduction to Computer Science Using Java" below to open the required reading listed.

Introduction to Computer Science Using Java Links to an external site.:

READ:

  • Chapter 8: Primitive Data
  • Chapter 9: Variables and the Assignment Statement

Primitive Data Type Self-Check Activity

Please match the phrases below.

 

Valid Identifier Sorting Activity

Please complete the activity below.

 

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS