Lecture 4, CS 302-7 and 8, January 30
i. Note – labs aren’t posted online until day of
i. http://pages.cs.wisc.edu/~cs302/resources/conSchedule.html
i. Compile-time
ii. Run-time
i. Type
ii. Name
iii. Value
i. <type> <name>;
i. <name> = <value>;
i. <type> <name> = <value>;
i. (integer) – for those familiar with integers from math
ii. Whole numbers – negative, positive, or zero
iii. Note – integers have a maximum size (demo with 3 billion)
1. MaxInt.java
2. System.out.println(1000000000*3);
3. Can use double or long instead
i. Like an int, but can store bigger numbers
ii. int is 4 bytes, long is 8 bytes
i. stores non-integer values, such as 0.5 or –1.2
i. If you just use the number instead of a variable, it is referred to as a literal
ii. Literal:
System.out.println(1+2);
iii. Variable:
int a=1+2;
System.out.println(a);
i. Start with letter or _. Cannot start with number
ii. Contain letters, numbers, and _
iii. No spaces, ?, %
iv. Case Sensitive
v. Not use reserved words
1. For instance, public, static, …
i. Begin with a lower case letter
1. Why? So that we don’t confuse them with classes, since classes should start with upper case
ii. Start new words with upper case
1. This is called ‘camel case’
iii. Be descriptive.
1. ‘x’ vs ‘fieldGoal’ - fieldGoal is a better name?
a. In case you forget what it means
b. In case anyone else ever has to work with your code
i. final int FIELD_GOAL=3;
i. Should use Constants wherever you have a ‘magic number’ in code.
1. Say, we had a complicated football program, and field goal is referenced a bunch of places in it.
2. Now, say the rules change, so a field goal is worth four points.
3. If we use a constant, we just have to change the value once, when we declare the constant.
4. If we were using 3, we would have to search our program for everywhere that uses 3 like that and change it.