Back

Activity 1.4 Variables

divider

Introduction

Activity 1.4

Variables

Topics

  • Introduction to Variables
  • Variable assignment
  • Static Typing
  • Primitive Types and Objects
  • Variable scope
  • Naming rules and conventions

Introduction to Variables

Variables allow us to store data during our program's execution. Sometimes, variables are described as containers for storing information.

Introduction to Variables

What data is being kept track of in this game?

Terminator Screenshot

Declaring a Variable

Using a type keyword, such as int, double, or String creates a variable and gives it a name. You my assign it a value later in the program.

int health;
double gpa;
String name;

Declaring a Variable

int health;
double gpa;
String name;

health = 100;
gpa = 4.2;
name = "Mortimer";

Declaring a Variable

You can also initialize a variable and give it a value immediately.

int health = 100;

Declaring a Variable

A variable can be used anywhere a typical value would be used.

int health = 100;
System.out.println("Health: " + health);
Terminal window
Health: 100

The Assignment Operator

The equal sign (=) used to assign a value to a variable.

It is not used to check for equality; it is only used for assigning a value to a variable.

Static Typing

Java is a statically typed language. Once a variable's type has been declared, it cannot change.

String value = 5;
int x = 5;
x = "5";

Primitive Types and Objects

A primitive type is a basic, fundamental piece of data that represents a single value.

Integer Types
byte 8-bit integer, used for very small numbers ranging from -128 to 127.
short 16-bit integer, for slightly larger numbers ranging from -32,768 to 32,767.
int 32-bit integer, this is the most common type for whole numbers (~ -2.4 billion to 2.4 billion).
long 64-bit integer, used for very large numbers that are bigger than an int can hold.

Primitive Types and Objects

A primitive type is a basic, fundamental piece of data that represents a single value.

Floating-Point Types
float 32-bit decimal number, used when memory is a concern. You must add an 'f' at the end of the number, like 3.14f.
double 64-bit decimal number. The default and most commonly used type for decimal values because it's more precise than float.

Primitive Types and Objects

A primitive type is a basic, fundamental piece of data that represents a single value.

Other Types
boolean Stores a single logical value, which can only be true or false.
char Stores a single character, like 'A' or '7'. It's a 16-bit unsigned Unicode character and is enclosed in single quotes.

Primitive Types and Objects

An object is a complex piece of data that can contain multiple values or pieces of information and has associated actions (called methods).

String is the first of many complex data types in this course.

String name = "Mortimer";
System.out.println(name + "is " + name.length() + " characters long.");
Mortimer is 8 characters long.
Object Screenshot

Variable Scope

Variables must be declared before they can be used.

System.out.println(health);
int health = 100;

Demo: Declare and Use Variables

public class Program {
public static void main(String[] args) {
int health;
int score = 14000;
int lives = 3;
String vehicle = "Motorcycle";
System.out.println(vehicle);
System.out.println("Lives left: " + lives);
System.out.println("Score: " + score);
}
}

Demo: Declare and Use Variables

public class Program {
public static void main(String[] args) {
int health;
int score = 14000;
int lives = 3;
String vehicle = "Motorcycle";
System.out.println(vehicle);
System.out.println("Lives left: " + lives);
System.out.println("Score: " + score);
}
}
Terminal window
Motorcycle
Lives left: 3
Score: 14000

Naming Rules and Conventions

Variables names cannot:

  • Contain spaces
  • Start with a number

String 1st; String first name;

For names containing multiple words, use camelCase.

String firstName;

Key Terms

Variable
A container that holds a value, such as a number or text, which can be changed while a program is running.
Variable Declaration
The process of creating a variable by specifying its type and giving it a name.
Assignment
The act of giving a value to a variable using the equals sign (=).
Static Typing
A rule that requires a variable's type to be declared and fixed when it is created, meaning it can only hold values of that specific type.

Key Terms

Primitive Types
Basic, fundamental data types in Java that represent single, simple values, like numbers or characters.
Objects
Complex data structures in Java that contain multiple values and have associated actions (called methods).
Variable Scope
The region or block of code where a variable can be accessed and used.

Key Terms

Naming Rule
A strict, syntax-based rule that must be followed when naming a variable (e.g., a variable name cannot start with a number).
Naming Convention
An informal, widely-accepted practice for naming variables to make code more readable (e.g., using camelCase for variable names).

'F' → Fullscreen

Objectives

  • icon Declaring and assigning values to variables.
  • icon Using variables in formatted strings.
divider

Activity Tasks

  • icon Create a new project named 1-4-Variables.
  • icon Complete each task individually.

Task 1: About Me

Program.java
public class Program {
public static void main(String[] args) {
// String Variables
System.out.println("- About Me -");
String person = "Anthony"; // Insert your name into the string
String food = "Pizza Hut Pepperoni Pizza"; // Replace pizza with your favorite food
System.out.println("Hello, my name is " + person + "!");
System.out.println("I, " + person + ", love to eat " + food + ".");
System.out.println();
//
}
}

Task 2: Tongue Twister

Program.java
public class Program {
public static void main(String[] args) {
// String Variables
System.out.println("- About Me -");
String person = "Anthony"; // Insert your name into the string
String food = "Pizza Hut Pepperoni Pizza"; // Replace pizza with your favorite food
System.out.println("Hello, my name is " + person + "!");
System.out.println("I, " + person + ", love to eat " + food + ".");
System.out.println();
System.out.println("- Tongue Twister -");
String item = "hot dogs"; // Insert a generic item
System.out.println("Try saying this five times fast:");
System.out.println("Peter Piper picked a peck of pickled " + item + ".");
System.out.println("A peck of pickled " + item + " Peter Piper picked.");
System.out.println("If Peter Piper picked a peck of " + item + ",");
System.out.println("where's the peck of " + item + " Peter Piper picked?");
System.out.println();
//
}
}

Task 3: Health Demo

Program.java
public class Program {
public static void main(String[] args) {
// String Variables
System.out.println("- About Me -");
String person = "Anthony"; // Insert your name into the string
String food = "Pizza Hut Pepperoni Pizza"; // Replace pizza with your favorite food
System.out.println("Hello, my name is " + person + "!");
System.out.println("I, " + person + ", love to eat " + food + ".");
System.out.println();
System.out.println("- Tongue Twister -");
String item = "hot dogs"; // Insert a generic item
System.out.println("Try saying this five times fast:");
System.out.println("Peter Piper picked a peck of pickled " + item + ".");
System.out.println("A peck of pickled " + item + " Peter Piper picked.");
System.out.println("If Peter Piper picked a peck of " + item + ",");
System.out.println("where's the peck of " + item + " Peter Piper picked?");
System.out.println();
// Number Variables
System.out.println("- Health Demo -");
int health = 100;
int damage = 55;
System.out.println("You've taken " + damage + " damage. You have " + (health - damage) + " health left.");
System.out.println();
//
}
}

Task 4: Age Demo

Program.java
public class Program {
public static void main(String[] args) {
// String Variables
System.out.println("- About Me -");
String person = "Anthony"; // Insert your name into the string
String food = "Pizza Hut Pepperoni Pizza"; // Replace pizza with your favorite food
System.out.println("Hello, my name is " + person + "!");
System.out.println("I, " + person + ", love to eat " + food + ".");
System.out.println();
System.out.println("- Tongue Twister -");
String item = "hot dogs"; // Insert a generic item
System.out.println("Try saying this five times fast:");
System.out.println("Peter Piper picked a peck of pickled " + item + ".");
System.out.println("A peck of pickled " + item + " Peter Piper picked.");
System.out.println("If Peter Piper picked a peck of " + item + ",");
System.out.println("where's the peck of " + item + " Peter Piper picked?");
System.out.println();
// Number Variables
System.out.println("- Health Demo -");
int health = 100;
int damage = 55;
System.out.println("You've taken " + damage + " damage. You have " + (health - damage) + " health left.");
System.out.println();
System.out.println("- Age Demo -");
int age = 35; // Assign your age
System.out.println("you will reach age 100 in " + (100 - age) + " years.");
System.out.println();
//
}
}

Task 5: Money Demo

Program.java
public class Program {
public static void main(String[] args) {
// String Variables
System.out.println("- About Me -");
String person = "Anthony"; // Insert your name into the string
String food = "Pizza Hut Pepperoni Pizza"; // Replace pizza with your favorite food
System.out.println("Hello, my name is " + person + "!");
System.out.println("I, " + person + ", love to eat " + food + ".");
System.out.println();
System.out.println("- Tongue Twister -");
String item = "hot dogs"; // Insert a generic item
System.out.println("Try saying this five times fast:");
System.out.println("Peter Piper picked a peck of pickled " + item + ".");
System.out.println("A peck of pickled " + item + " Peter Piper picked.");
System.out.println("If Peter Piper picked a peck of " + item + ",");
System.out.println("where's the peck of " + item + " Peter Piper picked?");
System.out.println();
// Number Variables
System.out.println("- Health Demo -");
int health = 100;
int damage = 55;
System.out.println("You've taken " + damage + " damage. You have " + (health - damage) + " health left.");
System.out.println();
System.out.println("- Age Demo -");
int age = 35; // Assign your age
System.out.println("you will reach age 100 in " + (100 - age) + " years.");
System.out.println();
System.out.println("- Money Demo -");
double balance = 100.25;
double withdraw = 45.11; // Replace 0 with a positive number of your choice.
double leftover = balance - withdraw;
System.out.println("You have a balance of $" + balance + " in your bank account.");
System.out.println("If you take out $" + withdraw + ", you will have $" + leftover + " left.");
//
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
- About Me -
Hello, my name is Anthony!
I, Anthony, love to eat Pizza Hut Pepperoni Pizza.
- Tongue Twister -
Try saying this five times fast:
Peter Piper picked a peck of pickled hot dogs.
A peck of pickled hot dogs Peter Piper picked.
If Peter Piper picked a peck of hot dogs,
where's the peck of hot dogs Peter Piper picked?
- Health Demo -
You've taken 55 damage. You have 45 health left.
- Age Demo -
you will reach age 100 in 65 years.
- Money Demo -
You have a balance of $100.25 in your bank account.
If you take out $45.11, you will have $55.14 left.
- About Me -
Hello, my name is Anthony!
I, Anthony, love to eat Pizza Hut Pepperoni Pizza.
- Tongue Twister -
Try saying this five times fast:
Peter Piper picked a peck of pickled hot dogs.
A peck of pickled hot dogs Peter Piper picked.
If Peter Piper picked a peck of hot dogs,
where's the peck of hot dogs Peter Piper picked?
- Health Demo -
You've taken 55 damage. You have 45 health left.
- Age Demo -
you will reach age 100 in 65 years.
- Money Demo -
You have a balance of $100.25 in your bank account.
If you take out $45.11, you will have $55.14 left.
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. How do variables make your code easier to read and understand?
  2. Why is it important to choose a descriptive name for a variable, like health or age, instead of a simple name like h or a?
  3. Based on the "Health Demo" and "Money Demo" tasks, how does using a variable allow you to perform calculations and show different results without changing the code inside the System.out.println() statement?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete