Download presentation
Presentation is loading. Please wait.
1
Variables ICS2O
2
Learning Goals Students will:
Describe the types of data that computers can process and store. Use variables, expressions, and assignment statements to store and manipulate numbers and text in a program.
3
What is a variable? A variable is a container that can hold information and can change throughout the life of the program. At the start of a program: x may hold the value 1 In the middle of the program: x may change to 5 At the end of the program: x may change to 0 Each variable represents a specific memory location (address) where the information can be stored. Variables in programming are very similar to variables in mathematics. The difference is that in programming variables can hold data other than numbers. Also, some statements in math are valid, but invalid in programming. E.g. x = 5 is the same as 5 = x in math. But in programming 5 = x is invalid and will crash the program.
4
Why do we need variables?
Interactivity: The exact same program/code can run multiple times with different results. E.g. Angry Birds Interactive vs non-interactive. The difference between a movie and a video game. Readability: Programs are easier to read Variables can have meaningful names. When you see 3.14 in a program, is it just a value or does it really represent pi? Numbers without name or meaning are called Magic Numbers and are stylistically unacceptable, with few exceptions
5
Three Parts to a Variable
A variable is like a container in a computer’s memory The type of variable is like the shape of the container. The variable identifier is like a label on the container. The value is what is stored inside the container Ovechkin (String) name 30 (int) age
6
The Primitive Types Assigning data types allows us to perform operations on data in a predictable manner. E.g. adding two numbers gives a number. Adding a number and text will produce unexpected results. Types of values that variables can hold are: Integers (int) – positive and negative whole numbers. Ex. … -2, -1, 0, 1, 2, … Decimal values double – positive and negative numbers with decimals. Ex. -2.5, -0.45, 51.6, float – same as double but takes up half the space in memory Ex. You must use an f to denote a float to Java, -2.5f, 51.6f, 7f Boolean (boolean) – contains the value “true” or “false”. There are other similar types to those above listed in a table here:
7
Object Data Types An Object is something that is made up of information (attributes) and can perform actions (behaviours) For example, a human is an object. Each person is a single instance of that general human design. We differ in our attributes: eye colour, skin tone, height, gender, etc. But we all perform the same basic actions: eat, sleep and poop. Strings are an object data type that holds text and therefore cannot be represented by a primitive data type. E.g. “Bob”, “555 Autumn Hill Blvd.”
8
What type? The Taken King There are 28 students in this class.
9
The Identifier Every variable must have a name so we can refer to it later, the same reason you have a name Identifier Rules: The identifier (name) must … Consist of letters and numbers Be unique and match no other variables or Java keywords Not start with a number Not contain spaces or special characters *, etc.) Not contain underscores (Although technically legal, its bad style)
10
The Identifier Identifier Conventions: The identifier (name) should …
Be short and descriptive of its specific purpose E.g. x is not effective whereas playerX would be effective when describing a player’s location in a video game Use camelCase – Since spaces are not allowed in naming we separate words by starting each word with a capital, except the first word which starts with a lower case letter. Ex. myFavoriteGame, studentNumber
11
Declaring/Defining When we want to create a new variable we use the terms define or declare to describe the process. Defining a new variable adds it to the Java dictionary for the life of the variable (usually the length of the program or code block it is defined in) It also reserves space in memory, similar to reserving a table at a restaurant. It is left empty until people(data) show up. A variable must be defined before it can be used in any way For this reason it is common practice to define your variables above any of our program logic since our programs run from top to bottom Variables are defined as either Global variables or Local Variables, this is determined by where we define the variable
12
Declaring/Defining Variables
As previously stated, variables need to be defined before their use. Defining/Declaring a variable requires a type and identifier and if the starting (initial) value is known this can optionally be added to the definition. Syntax: type identifier; E.g. String name; OR (Shortcut if you know the starting value) type identifier = expression; String name = “Bob”; Parts of the statement type - type of variable as described previously. identifier - the name of the variable using the rules and conventions given. expression - literal value, variable or evaluation to a result (optional for definitions) The expression MUST result in a value of the same type as the variable
13
Global vs. Local public class Main { public static void main(String[] args) } Local: Any variable defined inside a code block, such as main, is considered local A local variable remains in memory and is usable until the closing bracket } of the code block is reached during execution. It is then deleted. We should strive to have all of our variables local, as it frees up memory throughout the program’s life. Global: Any variable defined INSIDE the class, but OUTSIDE a code block (usually just below the class line) is considered global. These variables are in memory and usable for the ENTIRE life of the program In order to define a variable in the global section you need to add the keyword static before the definition so it is usable in the static main code block for compatibility reasons E.g. static int userAge; Define global variables here Define local variables here Write program logic here
14
The Value The variable can be assigned a value when it is declared and/or throughout the program as needed. Analogy: When you were born, your parents assigned you a value, your name. You have the option later in life to change that name if you wish.
15
Assignment Statements
To assign a value to a variable we use the = sign In programming = does not mean “equal to”, it means “assign the value” Syntax: NOTE the identifier MUST be on the left side of the = sign identifier = expression; EXAMPLE: int number; int number2; number = 5; Read as “Assign the value 5 to the variable called number” number2 = number + 5; How does the computer handle the last line? It evaluates the right side of the = sign before assigning the value to the variable on the left. It finds the value of number in memory and substitutes its value in the place of the variable Performs the math (5 + 5), resulting in 10 Then assigns the value 10 to the variable, number2.
16
Example Program The code below represents a new empty program
For now (to be updated later), we will define all of our variables INSIDE and AT THE TOP of the main code block public class Main { public static void main(String[] args) } Simple program example int userAge = 16; String userName = “Judy”; System.out.println(“The user’s name is “ + userName + “ and he/she is “ + userAge + “.”; Define global variables here Define local variables here Write program logic here Outputs: “The user’s name is Judy and he/she is 16.”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.