Variables and Data Types
What is a variable?: 1.3.1
- A variable is a name associated with a memory location
- Stores a value which can vary
How to create a variable in Java
- Identify the type
- 3 Main types you need to know for the CSA Exam
int
- Whole number
double
- Decimal value
boolean
- true or false
- identify the name
- identify the initial value
- You don’t have to do this in all cases
int myVariable = 0; // Declaring a variable without a value
boolean foo; // Declaring a variable without a value
foo = true; // Assigning a value to an existing variable
Data Types: 1.3.2
- Two types of variables in Java
- Primitive variables
- Object or reference variables
- Hold a reference to an object of a class
- A reference is a way to find the object
- Hold a reference to an object of a class
- 3 Primitive types are on the AP CSA Exam
int
- Stores integers (whole numbers)
- stores values from -231 to 231-1 (32 bit)
double
- stores floating point numbers (decimal numbers)
- 64 bit
boolean
- Stores boolean values (true or false)
String
is an object type on the exam- The name of a class
- A sequence of characters enclosed in double quotes
- “Hi there!! :)”
- A type is a set of values (a domain; think math class!!) and a set of operations which can be done on them
- You can add together integers, but can’t add booleans
Declaring variables: 1.3.3
- To create a variable, Java needs to know the data type and the name
- called “declaring a variable”
- Type is a keyword like
int
,double
, orboolean
- You make up a name
- When you declare a primitive variable…
- Java allocates (sets aside) enough bits of memory for that type
- Associates the memory address with the name you chose
- Computers store all values as bits (binary digits)
- Each bit has two values
- Conventionally called either 1 or 0
- Each type requires a different number of bits, and each type gets represented differently
integer
has 32 bits of memorydouble
has 64 bits of memoryboolean
only needs 1 bit of memory
- Each bit has two values
How to declare a variable
- Specify the type
- Specify the name
int score;
double health;
boolean isRunning;
After a variable has been declared, it can be assigned a value using =
double health;
health = 0.88;
An initial value can also be set during declaration
double health = 0.88;
How to print a variable
- Use the string concatenation operator
+
- You can’t put a variable inside quotes
- Will just print the variable letter
String myName = "Nathan";
System.out.println("myName"); //Outputs: myName
System.out.println(myName); //Outputs: Nathan
Assignment Operator
- In Java,
=
is the assignment operator- Doesn’t mean both sides are equal
- Sets the value on the left with a copy of the value on the right
Constants
- The keyword
final
can be used in front of a variable declaration to make a value constant- It cannot be reassigned
- These variable names are conventionally capitalized
final double STANDARD_GRAVITY = 9.81; // m/s^2
Naming Variables: 1.3.4
- Variables should start with an alphabetic character
- Can include letters, numbers, and underscores
- Must be all 1 word (no spaces)
- Cannot be a keyword/reserved word
- The variable should be meaningful
- When keeping track of a score,
score
makes more sense thanx
- When keeping track of a score,
- Avoid really long names
- Use camelCase
- This is convention in Java
- first word is lower case, and every word after has the first letter capitalized
- Looks like camel humps :)
- Can also use underscore to seperate words
- Can’t use spaces
- Variables are case sensitive!
myvar
is not the same asmyVar
Summary: 1.3.6
- A variable is a name for a memory location where you can store a value
- A variable is declared like so:
int score;
- And initialized:
double gpa = 3.5;
- Data types can be primitive (int/boolean/double) or reference types (String, etc.)
- Variables have associated memory locations which hold their values
- Memory associated with primitive types hold actual values
- When a variable is declared with
final
, it is constant and cannot be changed
AP Practice: 1.3.7
Nothing to note here, just complete the task.
Closing thoughts
Overall I think the section was a good introduction to variables and data types. In particular, I really liked the comparison of calling the possible set of values of a type the domain, because I think it creates a valid link between Mathematics and Computer Science. It definitely wasn’t an important part of the lesson, I just have always found that link to be cool :). In spite of that, I don’t know why every other primitive type was ignored completely. While I understand that AP doesn’t require a student to know about them, I find it odd that they weren’t at least acknowledged as basic features of the language.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.