2. Variables¶
2.1. Objectives¶
- Read and write statements using variables
2.2. Time Goal¶
- 20 minutes on this section
2.3. Key Terms¶
- variable
- A named storage location for values. All variables have a type, which is declared when the variable is created.
- value
- A number, string, or other data that can be stored in a variable. Every value belongs to a type (for example, int or String).
- identifier
- A name created by a programmer for an item like a variable or method
- type
- Mathematically speaking, a set of values. The type of a variable determines which values it can have.
- declaration
- A statement that creates a new variable and specifies its type.
- keyword
- A reserved word used by the compiler to construct programs. You cannot use keywords (like
public
,class
, andvoid
) as variable names.
2.4. Exercises¶
Note
One of the most powerful features of a programming language is the ability to define and manipulate variables. A variable is a named location in memory that stores a value. Values may be numbers, text, images, sounds, and other types of data. To store a value, you first have to declare a variable.
String message;
This statement is called a declaration, because it declares that the variable named message has the type String. Each variable has a type that determines what kind of values it can store. For example, the int
type can store integers, and the char
type can store characters.
Some types begin with a capital letter and some with lowercase. We will learn the significance of this distinction later.
To declare an integer variable named x, you simply type:
int x;
Note that x
is an arbitrary name for the variable. In general, you should use names that indicate what the variables mean.
String firstName;
String lastName;
int hour, minute;
This example declares two variables with type String
and two with type int
. The last line shows how to declare multiple variables with the same type: hour and minute are both integers. Note that each declaration statement ends with a semicolon ;
.
Variable names usually begin with a lowercase letter, in contrast to class names (like Hello
) that start with a capital letter. When a variable name contains more than one word (like firstName
), it is conventional to capitalize the first letter of each subsequent word. Variable names are case-sensitive, so firstName, firstname, and FirstName are all different variable names.
Note
Rules for an identifier
- An identifier in Java must be a sequence of letters (a-z, A-Z), underscore (_), dollar signs ($), and digits (0-9)
- Must start with a letter, underscore, or dollar sign
-
Q-86: Which of the following would be considered a properly declared and named variable for a text value of the day of the week?
int day;
- Incorrect
String DayOfTheWeek;
- Incorrect
string day/of/the/week;
- Incorrect
String dayOfTheWeek;
- Correct
String weekDay
- Incorrect
-
Q-87: Which of the following would be considered a valid variable name for a double value for the ratio of sales to revenue in a program?
r
- Correct
_ratio_sales_
- Correct
RATIOSALES
- Correct
void
- Incorrect, this is a keyword and cannot be used as a variable
ratioOfSalesToRevenue
- Correct
-
Q-88: What type should you use to represent the number of people in a household?
- int
- The number of people is a whole number so using an integer make sense.
- double
- Can you have 2.5 people in a household?
- boolean
- Is the number of people something that is either true or false?
- String
- While you can use a string, a number is better for doing calculations with (like finding the average number of people in a household).
- Object
- An object is a generic data type that doesn't represent any specific type of data storage.
-
Q-89: What type should you use to represent the amount of money you have?
- int
- The integer type (int) can't be used to represent decimal numbers so you couldn't use it if you had any cents.
- double
- The double type can be used to represent an amount of money.
- boolean
- Java uses boolean for values that are only true or false.
- String
- While you can use a string to represent the amount of money you have it is easier to do calculations on the numeric types (int or double).
-
Q-90: What type should you use to record if it is raining or not?
- int
- While you could use an int and use 0 for false and 1 for true this would waste 31 of the 32 bits an int uses. Java has a special type for things that are either true or false.
- double
- Java has a special type for variables that are either true or false.
- boolean
- Java uses boolean for values that are only true or false.
- String
- While you can use a string to represent "True" or "False", using a boolean variable would be better for making decisions.
-
Drag the term to the correct statement
- variable
- A named storage location for values
- value
- 4 is an example
- declaration
- A statement that creates a new variable and specifies its type
- keyword
- A word reserved in a programming language
-
Q-91: Based on the above, what would be the best variable name for storing information about someone’s birthday?
- datBrt
- Incorrect
- dateOfBirth
- Correct
- date_of_birth
- Incorrect
- dateofbirth
- Incorrect
- _birth_day
- Incorrect
2.4.1. Keywords¶
You can use any name you want for a variable. But there are about 50 reserved words, called keywords, that you are not allowed to use as variable names.
Note
Java Language Keywords
abstract | continue | for | new | switch |
assert | default | goto | package | synchronized |
boolean | do | if | private | this |
break | double | implements | protected | throw |
byte | else | import | public | throws |
case | enum | instanceof | return | transient |
catch | extends | int | short | try |
char | final | interface | static | void |
class | finally | long | strictfp | volatile |
const | float | native | super | while |
Please type true or false the statement
myObject is a valid variable name
Please type true or false the statement
native is a valid variable name
Please type true or false the statement
Native is a valid variable name
Note
Add the necessary declarations indicated by comments so that the correct output is printed. Run the code once before making any changes to see what types of errors you get. Do not change any of the other code. Your output should be:
My name is Suzy
My age is 19
Note