1
|
- Announcements
- hw2 due Thursday, February 12,
when labs close
- Read Chapter 2 of JOI
- Questions (please!)
- Agenda
- where does the world begin?
- tokens, keywords and identifiers
- arithmetic
- directory/folder structure (time permitting)
|
2
|
- %> java Foo starts execution at main method in class Foo
- There are no objects yet
- main (and other methods) create objects with new, then messages can
begin (Bank)
- Most classes don’t have main (BankAccount)
- Some classes have only main
(Hello)
|
3
|
- Token: smallest sequence of characters that makes sense by itself (like
a word in English)
- atm.println(“sorry... ”);
- The seven tokens on this line are
- atm .
println ( “sorry... ” )
;
- White space (blank, tab, newline) may (but need not) separate
tokens: 2+3 and 2 + 3 are the same (but the last is bad
form)
|
4
|
- Words whose meaning is fixed by Java
- Can’t be reused by the programmer
- Only 48 of them
- if class int new while public …
- Listed in JOI and in Sun’s Java tutorial (link from course home page)
- true, false and null also have
fixed meanings, although they are not keywords
- Punctuation tokens also have fixed meanings
|
5
|
- Tokens (words) we make up to use in code: names for classes, fields,
methods, variables
- Start with letter, then letters and numbers and some other characters
- Terminal atm
CS110 String whichAccount Bank MAXVALUE
- Conventions ...
|
6
|
- Class names start with upper case:
Bank, BankAccount, Terminal, String
- Some classes, like String, come with Java, but make String is not a
keyword. We could write our own String
class if we wished
- Field, method and variable names start lower case: account1, getBalance,
moreTransactions
- Class, field and variable names should be nouns, method names should be
verbs
- Internal words capitalized
- JOI has more detail …
|
7
|
- What are the tokens in the following line of Java code?
- Which of these tokens are keywords?
- Which of these tokens are identifiers?
- Which of these tokens name classes?
- Which of these tokens name objects?
- Practice on the code you read every day
- Quiz your friends
|
8
|
- Variables store values
- A field is one kind of variable
- Fields in an object keep track of object’s state
- String bankName // Bank.java
- int balance; //
BankAccount.java
- Variables in a method store values needed while that method runs – for
example, in Bank.java
boolean moreTransactions;
// line 105 int
amount; // line 119
|
9
|
- You must tell the compiler the kind of value you want to store, and the name
of the place you will store it
- Declaration syntax: type name;
- type tells the compiler what kind of value you want to store
- name is the identifier you choose so that you can refer to the variable
elsewhere in the program
- Declaration may (but need not) initialize value
|
10
|
- in the class description, outside any methods
- at the top of the class (by convention)
- 20 public class Bank
- 21 {
- 22 private String bankName;
- 24 private Terminal atm;
- 26 private BankAccount
account1;
- private BankAccount account2;
- 29 private static final int INITIAL_BALANCE = 200;
- for now, ignore public,private, static, final
|
11
|
- inside the method where they are used
- near where they are first used (our convention) or at the top of the
method (another convention)
- 57 public void open()
- 58 {
- atm.println( "Welcome to
“ + bankName );
- 60 boolean bankIsOpen = true;
- no public/private for method variables
|
12
|
- primitive (int, boolean, ...)
- just eight of them, built into language
- names are Java keywords
- in box-and-arrow pictures, values are in the box
- reference (Bank, Terminal, ...)
- these are the objects in OOP
- names are the names of classes
- there are as many classes as you care to invent
- some come shipped with Java (String, …)
- in box-and-arrow pictures, values are arrows (references) in the box
|
13
|
|
14
|
- x = y means
“make the value of x the value of y !”
- Example (primitive types)
- int y = 6; // declare y, initialize value
- int x; // declare x
- x = y; // copy content of box y into box x
- For reference types, x = y means arrow in x now points to the same place
as arrow in y.
- x = y is not the same as y = x
- This makes mathematicians unhappy
|
15
|
- x == y means
“is the value of x the same as the value of y?”
- Bank.java line 82: if (accountNumber
== 1 )
- x == y is the same as y == x
|
16
|
- primitive type int (also short, long)
- there are maximum and minimum values
- +, - , * do what you expect them to
- so do >, >= , <, <=, ==
- use () to alter order of operations
- / truncates: 20/6 is 3
- % (modulus operator) gives remainder: 20 % 6 is 2
|
17
|
- primitive type double (also float)
- there are maximum and minimum values
- 6.023E23 is acceptable input
- +, - , *, >, >= , <, <=, ==
do what you expect them to
- use () to alter order of operations
- you can mix int and double in formulas
- / works properly (20.0/6 is 3.33333…)
|
18
|
- Primitive type boolean
- Just two values true and false
- Use in tests (if and while)
|
19
|
- Class String predefined in Java
- A string is an object, not a primitive type
- Can use = for assignment
- String greeting = “Hello,
world”;
- Can’t use == for equality, since Strings are objects
- Test equality by sending an equals message:
- if (command.equals(“exit”)) {
|
20
|
|
21
|
|