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

  1. Identify the type
    • 3 Main types you need to know for the CSA Exam
    1. int
      • Whole number
    2. double
      • Decimal value
    3. boolean
      • true or false
  2. identify the name
  3. 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
  • 3 Primitive types are on the AP CSA Exam
    1. int
      • Stores integers (whole numbers)
      • stores values from -231 to 231-1 (32 bit)
    2. double
    3. 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, or boolean
    • 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 memory
      • double has 64 bits of memory
      • boolean only needs 1 bit of memory

How to declare a variable

  1. Specify the type
  2. 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 than x
  • 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 as myVar

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.