Transcript Document

CS110 Lecture 4
Thursday, February 5, 2004
• 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)
Lecture 4
1
Where does the world begin?
• %> 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)
Lecture 4
2
Reading Java
• 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)
Lecture 4
3
Keywords
• 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
Lecture 4
4
Identifiers
• 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 ...
Lecture 4
5
Identifier conventions
• 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 …
Lecture 4
6
Exam question
• 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
Lecture 4
7
Fields and variables
• Variables store values
• A field is one kind of variable
• Fields in an object keep track of object’s state
String bankName
int balance;
// Bank.java
// BankAccount.java
• Variables in a method store values needed while
that method runs – for example, in Bank.java
boolean moreTransactions;
int amount;
Lecture 4
// line 105
// line 119
8
Declaring variables
• 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
Lecture 4
9
Fields are declared …
•
•
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;
27
private BankAccount account2;
29
private static final int
INITIAL_BALANCE = 200;
•
for now, ignore public,private,
static, final
Lecture 4
10
A method’s variables are declared …
•
•
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 {
59
atm.println( "Welcome to “ +
bankName );
60
boolean bankIsOpen = true;
•
no public/private for method variables
Lecture 4
11
Types of variables
• 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
Lecture 4
12
Box-and-arrow pictures
Bank
String
bankName:
type (reference ) BankAccount
account1:
name of field
int
int
balance: 200
999
name of field
BankAccount
BankAccount
account2:
type (primitive ) int
int
balance: 200
200
Terminal
atm:
Strings are objects
“Engulf and Devour”
BankAccount
value
Terminal
When value is an object, use an arrow to point to (refer to)
Lecture 4 it’s in the box.
that object . When it’s primitive
13
=
• 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
Lecture 4
14
==
• 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
Lecture 4
15
Integer arithmetic
• 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
Lecture 4
16
Decimal arithmetic
• 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…)
Lecture 4
17
True or false?
• Primitive type boolean
• Just two values true and false
• Use in tests (if and while)
Lecture 4
18
Strings
• 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”)) {
Lecture 4
19
Lecture 4
20
Lecture 4
21