๐Ÿ““1.2: Variables & Data Types

Table of Contents


๐Ÿ“– This page is a condensed version of CSAwesome Topic 1.2


What is a Variable?

Variable
A labeled memory location in the computer that can store a value that can change or vary while a program is running.

When you play a game, it will often have a score. Scores often start at 0 and increase, so they can change. A score can be stored in a variable.

image-small

INTRODUCTORY ACTIVITY

๐ŸŽฎ๐Ÿ“ฑ As a class, brainstorm some examples of important pieces of information (data) in video games that you play (or apps you use in general).

  • How/why would a program be able to be personalized for each player that plays the game?
  • What kind of information (data) does the game need to remember about each player?

Data Types

Every variable has a name and a data type that determines the kind of data it can hold.

There are two types of variables in Java:

  • Primitive variables that hold one simple unit of data, like a number
  • Object (reference) variables that hold a reference to a more complex set of data

    A reference is a way to locate the object (like a UPS tracking number helps you find your package)

Primitive Types tested on the AP Exam:

  • int which can represent integers, i.e. numbers with no fractional part such as 3, 0, -76, and 20393.
  • double which can represent decimal numbers like 6.3, -0.9, and 60293.93032.

    Computer people call these โ€œfloating pointโ€ numbers because the decimal point โ€œfloatsโ€ relative to the magnitude of the number, similar to scientific notation like 6.5 ร— 10^8.

  • boolean which can represent only two possible values: true or false.

    The data type is named for George Boole.

String is one of the object (reference) types on the exam and is the name of a class in Java. A String is written in a Java program as a sequence of characters enclosed in a pair of double quotes โ€” like "Hello".

A data type is a set of possible values (a domain) but also a set of operations on them.

For example, you can do addition operations with ints and doubles but not with booleans or Strings.

๐Ÿ’ฌ With the people at your table, discuss the situations below. Select the appropriate data type out of the following choices: int, double, boolean, String

  • What type should you use to represent the average grade for a course?
  • What type should you use to represent the number of people in a household?
  • What type should you use to hold the first name of a person?
  • What type should you use to record if it is raining or not?
  • What type should you use to represent the amount of money you have?

Declaring Variables in Java

Creating a new variable is also called declaring a variable. The type must be a recognized keyword like int, double, or boolean, but you get to make up the name for the variable.

When you create a primitive variable Java will set aside enough bits in memory for that primitive type and associate that memory location with the name that you used.

Computers store all values using bits (binary digits). A bit can represent two values and we usually say that the value of a bit is either 0 or 1.

image

Examples of variables with names and values. Notice that the different types get a different amount of memory space.

image

DECLARE

To declare (create) a variable, specify the type, leave at least one space, then the name for the variable, and end the line with a semicolon (;).

int score;

ASSIGN

After declaring a variable, you can assign a value to it using an equals sign = followed by the value:

score = 4;

INITIALIZE

Or you can combine both steps into one line, declaring the type AND setting an initial value for a variable:

int score = 4;

Concatenation

image-small

When you are printing out variables, you can use the string concatenation operator + to combine them with more text inside a System.out.print statement.

Never put variables inside quotes ""! You do not want to print out the variable name, but the value of the variable in memory.

In your Java file in GitHub Codespaces, type and run each line below in the main method. Observe the output, then change values, add/remove spaces, and (on purpose) put quotes around a variable name to see what happens.

double price = 23.25;
System.out.print("The price is ");
System.out.println(price);

String name = "Bob";
System.out.println("Hi " + name);


  1. Type the (incorrect) assignment below, run it, and read the compiler error.
  2. Then fix it and run again.
int num;
4 = num;           
System.out.println(num);

Naming Variables

While you can name your variable almost anything, there are some rules. A variable name should always start with an alphabetic character and can include letters, numbers, and underscores _. It must be all one word with no spaces.

๐Ÿšซ You cannot use any of the keywords or reserved words as variable names in Java (for, if, class, static, int, double, etc). For a complete list of keywords and reserved words, see https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-3.9.

Variable Naming Guidelines:

  • Use a meaningful variable name that describe the data it holds.
  • Start variable names with a lowercase letter and use camelCase for names with multiple words.

    Variable names are case-sensitive and spelling-sensitiveโ€ฆ every use must match the original declaration!


Summary

  • (AP 1.2.B.2) A variable is a memory storage location that holds a value, which can change while the program is running.
  • (AP 1.2.B.2) Every variable has a name and an associated data type that determines the kind of data it can hold. A primitive variable holds a primitive value from that type.
  • Variables can be declared and initialized like:
int score;
double gpa = 3.5;
  • (AP 1.2.A.1) A data type is a set of values and operations. Data types can be primitive types (like int) or reference types (like String).
  • (AP 1.2.A.2) The primitive data types used in this course define the set of values and operations for numbers and Boolean values.
  • (AP 1.2.A.3) A reference type, like String, is used to define objects that are not primitive types.
  • (AP 1.2.B.1) The three primitive data types used in this course are int (integer numbers), double (decimal numbers), and boolean (true or false).
  • String is a reference data type representing a sequence of characters.

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.