Semester 1 review

Expressions

Parts of a program that produces a value.

Two kinds of expressions

  • Simple value expressions just produce a value.

  • Assignable expressions are expressions that can be assigned a value. When used for a value, their value is whatever value was last assigned.

Simple value expressions

Literal Directly specified value
Operator Value computed from operands
Method call Value returned by method
Constructor call New object
Array creation New array

Literals

  • int: 1, 1_000_000, -42

  • double: 1.5, 0.00001, 1e6

  • boolean: true, false

  • String: "foo", "hello, world!"

  • null

Operators

Low-level features of the language that operate on numbers, booleans, and references as references. We'll go through the ones we know about later in these slides.

Method call

Calling a method with a non-void return type is an expression whose value is the value returned by the method.

s.indexOf("abc")

Math.random()

s.length()

Constructor call

Calling a constructor (with the new keyword) is an expression whose value is a reference to the newly created object.

new Point(0, 0)

Array creation

There are two ways to create arrays with the new keyword, by specifying the dimensions of the array or by providing the initial values.

new int[10]

new int[] { 1, 2, 3 }

Note: Array initialization

Only in the context of a variable declaration we can write:

int[] nums = { 1, 2, 3 };

rather than:

int[] nums = new int[] { 1, 2, 3 }

This is just a bit of syntactic sugar.

Where value expressions go

int x = y * 10; Right side of an assignment.
y * 10 + 100 As an operand.
foo(y * 10) Method argument.
new Bar(y * 10) Constructor argument.
return y * 10; As a value to be returned.
xs[y * 10] Array and index in array access expression.

Assignable expressions

Assignable expressions are expressions that can appear on the lefthand side of an assignment operator because they represent a place where a value can be stored: various kinds of variables and the individual elements of arrays.

When they appear in a position where a value expression can occur, their value is the value they were most recently assigned.

Examples

x = 10 Assigning to a variable.
x += 10 Compound assignment.
obj.x = 10 Assigning to an instance variable.
Foo.x = 10 Assigning to a class (static) variable.
xs[0] = 10 Assigning to an array element.
xs[0]++ Incrementing an array element.

Local variables

Local variables are declared within a method or constructor. They do not have access modifiers like public or private because they are not accessible at all outside the method or constructor where they are declared.

x Accessing a variable
x = 10 Assigning a value to a variable

Instance and class variables

x.foo Instance variable access
x.foo = 42 Assigning to an instance variable
Math.PI Class (static) variable access
MyClass.something++ Incrementing a class (static) variable

Variable examples

public class Robot {
  // Class variable
  private static int nextSN = 0;
  // Instance variables
  private String name;
  private int serialNumber;

  public Robot(String name) { // Parameter is local variable
    // Asssign value of local variable to instance variable
    this.name = name;
    // Assign value of static variable to instance variable.
    serialNumber = nextSN++; // and update static var
  }
}

Array access

nums[0] Access the first element of array
nums[0] = 100 Assigning to the first element of array
nums[nums.length - 1] Access the last element of array

Parenthesized expressions

Parenthesized expressions are mostly used in for-value expressions to control the order of operations but they are technically assignable if the inner expression is.

(a + b) * c Control the order of operation
(array1)[0]++ Increment the first element of array1.

The value of (a + b) is the same as a + b. The parentheses just cause it to be evaluated first.

Operators

Operators, part 1

Arithmetic Operands numbers; result number
String Operands String and anything; result String
Logical Operands booleans; result boolean
Comparison Operands numbers; result boolean
Equality Operands anything; result boolean

Operators, part 2

Assignment Assign values to variables and array elements
Cast Convert between ints and doubles
Ternary Bonus operator. Not in AP curriculum.

Arithmetic

+ addition
- subtraction
* multiplication
/ division
% remainder

String

+ concatenation

Logical

&& logical and
|| logical or
! logical not

Comparison

< less than
> greater than
<= less than or equal
>= greater than or equal

Equality

== equal
!= not equal

A note on equality

Remember that for reference types (e.g. String and arrays) == just checks whether the two values are references to the same object that lives in the same place in memory; it doesn't do any deeper comparison.

Classes such as String often implement an equals method that does an appropriate comparison that allows two different objects to be consideder the same if they have the same contents.

Assignment

= simple assignment
+= add to variable
-= subtract from variable
*= multiply variable by
/= divide variable by
%= set variable to remainder by
++ increment
-- decrement

Cast

(int) cast to int
(double) cast to double

Ternary (?:)

A bonus operator. Like an if statement but can be used in expressions:

Color c = row % 2 == 0 ? RED : WHITE

First sub-expression must evaluate to a boolean. Then evaluates either the expression after the ? or the expression after the :, depending on whether the boolean expression is true or false.

Statements

Parts of a program that do things, mostly controlling the flow of the program.

Some statements

  • return

  • if, if/else

  • for (normal and enhanced)

  • while

Expression statements

Some (but not all) expressions can be used as a statements.

x = 10; Assignment
x++; Increment
x--; Decrement
someMethod(); Method call

Note the semicolon at the end of each statement.

Declarations

Parts of a program that create named elements.

They are neither expressions nor statements as they neither produce a value nor cause any computation to happen.

The ones you should know

  • Classes. Withn classes are:

    • Instance and class variables

    • Instance and class methods

    • Constructors

  • Within methods and constructors:

    • Method and constructor parameters

    • Local variables