Sample Main.java for a BankAccount problem
public class Main {
public static void main(String[] args) {
// Here is what happens when this line of code is executed:
BankAccount acct = new BankAccount(1000.55);
// new finds a place in memory to put a virtual BankAccount,
// places it there, and returns the memory location
// = is called an assignment operator; much like + adds numbers
// and * multiplies them, = is also an operator. The expression
// to the right of = is computed, and then the variable on the
// left of the = is assigned that value. Since new returns a
// memory location, acct is actually assigned a location in memory.
// We can see this by printing acct:
System.out.println(acct);
// The output is BankAccount@HEXADECIMAL_MEMORY_LOCATION
//
// Going back to the first line of code, the BankAccount
// on the far left does not actually have anything to do with
// what is placed in memory. Rather, when acct is used, it
// determines how acct is supposed to be viewed by Java. It
// probably seems trivial to say that acct should be a BankAccount,
// but later in the class, we will see situations where other
// approaches make sense. (The topic is "polymorphism" for
// hotshots who want to look ahead.)
}
}