Assignment statements: 1.4.1

  • Assignment statements init/change the value stored in a variable
    • use the assignment operator (=)
    • The value of the expression on the right is stored in the variable on the left
      • int health = (100 + HEALTH_POTION_PTS) * BONUS_MODIFIER;
      • “left gets/is assigned right”
  • We can copy a variable’s value to another variable
    • y=x
    • Does not link the variables together
      • If the value of x changes later on, y stays the same

Adding 1 to a Variable: 1.4.2

  • Add one to the current value of the variable
    • spam = spam + 1;
      • This works because the variable on the left is set to the expression on the right

Input with Variables: 1.4.3

Operators: 1.4.4

  • Java has standard mathematical operators
    • + : addition
    • - : subtraction
    • * : multiplication
    • / : division
  • Both doubles and ints can do arithmetic
  • Arithmetic between ints will produce an int
    int ham = 3;
    int spam = 2;
    System.out.println(ham/spam); // Output is 1
    
    double chicken = 3;
    double turkey = 2;
    System.out.println(chicken/turkey); // Output is 1.5
    
  • Arithmetic between doubles will produce a double
    double playerHealth = 0.36; // hp is out of 1
    final double HEALING_POTION = 0.64;
    playerHealth = playerHealth + HEALING_POTION;
    System.out.println(playerHealth); // Output is 1.0
    
  • == tests if values on left and right are equal
    • != tests for inequality
    • One and two equals signs mean different things
      • = is the assignment operator
      • == is the equality operator
    • Values need to be exactly the same
      • 3.3333 != 3.3334
        • Doubles can be very tricky! (See closing thoughts)
    • Be careful when comparing doubles, because they have limited precision
      • Infinitely repeating decimals and irrational numbers get rounded so it can be stored in memory
        • 1.0/3.0 becomes 0.3333333333333333
  • Be careful when dividing ints by zero
    • Results in an ArithmeticException
    • Dividing doubles by 0.0 results in Infinity
      • A result of IEEE 754
      • This is not in the curriculum, I just thought it was an interesting quirk :)
  • Operators can be combined to create compound expressions
    • Can use a literal values ( like 2) or variables
    • Evaluation of expressions uses operator prescience
      • Expressions in parenthesis are evaluated before moving outward
      • *, /, and % come next
      • Then + and -

The Modulo Operator: 1.4.5

  • % is the modulo (mod) operator
    • Finds remainder
      • 5 % 2 is 1

Programming Challenge - Dog Years: 1.4.6

This section is relatively straightforward. The only note I have is that you can use the String.format method to more cleanly print the output, but I don’t think that’s part of the exam.

System.out.println(String.format("My age: %d, Dog's age (dog years): %d", age, dogYearsAge));

Learn more about String.format here.

Summary: 1.4.7

  • Arithmetic expressions can include ints and doubles
  • Arithmetic Operators
    • + : addition
    • - : subtraction
    • * : multiplication
    • / : division
    • % : modulo (remainder)
  • Arithmetic between ints will result in an int
    • Decimal is thrown away
  • Arithmetic with at least one double will result in a double
  • Operators can be used to construct compound expressions
    • Follows operator precedence
      • *, /, % have precedence over + and -, unless parentheses are used to group those.
  • Dividing ints by 0 results in ArithmeticException
  • Assignment operator = initialized or changes the value stored in a variable
    • Right is stored in left
  • Expressions are evaluated at runtime to a single value
  • The value of an expression has a type based on the evaluation
    • 3/2 is an int
    • 3.0/2.0 is a double

AP Practice: 1.4.8

Nothing to note here.

Closing thoughts

My only criticism of the section is the assertion that the equality operator cannot be used for doubles. While doubles do have limited precision (which is worth noting), there is no universe where 3.3333 does equal 3.3334 in the first place, which made it a very odd example. In my opinion it would make more sense to acknowledge that irrational and infinite decimals are rounded to fit within the finite 64 bits provided by doubles and leave it at that.

As it turns out, I had not gone far enough down the “representing non integer numbers using computers” rabbit hole. On top of the issues of rounding infinitely repeating/irrational decimals, computers also typically represent numbers in base 2 (binary!!) instead of base 10. This causes further problems, as normally finite numbers (such as 0.1) are now infinite.

This still isn’t a topic I have a good understanding of, but both python’s docs and a blog post from Lahey Computer Systems do a better job of explaining the issue that I ever could.