Variable Types
Overview
Students categorize information as a String, int, or boolean and practice declaring and initializing.
Each student interacts with others to find the answer to a question you wrote. They then learn how they would write code to save that piece of information (the answer) in a variable.
Before Class
Materials: pencils and pieces of paper (1 per student), whiteboard, snacks (optional)
Prep: Write a question about a student on a piece of paper. The answer should be a boolean, String, or int. For example: “How old is Alex?”, “Is Alex an astronaut?”, or “What is Alex’s favorite food?” Repeat so that there is one piece of paper about each student.
Class Time
Pass out the pieces of paper to students, making sure that no one has the question about themselves. Ask them to find out the answer to the question, then write it down.
Review with students what booleans, Strings, and ints are. Ask students to decide whether the answer they wrote down would be a String, boolean, or int in Java.
Review how to declare and initialize a variable. On the whiteboard, do an example for each variable type. You may want to cover these points:
- Variable names can’t have spaces in them
- String values have double quotes around them, but variable names don’t
- Strings start with a capital letter, while ints and booleans don’t
- = means “gets a new value”
- ; means “we’re done with this”, like how we end sentences with a period in English
- The variable name should describe what information it holds
Ask students to declare and initialize a variable to hold the information they gathered. For example, this might look like
int alexAge = 10; boolean isAlexAstronaut = false; String alexFavoriteFood = “broccoli”;
Have students bring their paper to a teacher when they are complete. Correct syntax and a good variable name merits a snack!