Lecture 4, CS 302-6, September 12
i. Compile-time
ii. Run-time
i. Not 100% true, actually – some variables (eg strings) are pointers. More on this later
i. Type is assigned at declaration – eg int, double, etc
ii. int number;
i. Assigned a starting value
ii. number=10;
i. int number=10;
i. (integer) – for those familiar with integers from math
ii. Whole numbers. -1,0,1,2, etc
iii. Note – max size of integer (demo with 3 billion)
1. System.out.println(1000000000*3);
2. Can use double or long instead
3. Integer actual size – 32 bit (4 byte), actual max - 2,147,483,647
i. Floating point numbers
1. Similarity between 1.36, 13.6, 136, etc.
2. 1.36=.136*10^1, 13.6=1.36*10^2, etc
i. Refers to using number values in code
ii. Bad because you have to go back and change all values if something changes with the initial conditions
i. For instance, “1,000 ½” would not be valid
i. Start with letter or _. Cannot start with number
ii. Contain letters, numbers, and _
iii. No spaces, ?, % etc
iv. Case Sensitive
v. Must not use reserved words
i. Start variable name with lower case
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. The textbook calls this ‘camel case’
iii. Be descriptive.
1. ‘x’ vs ‘fieldGoal’ - Why is fieldGoal a better name?
a. In case you forget what it means
b. In case anyone else ever has to work with your code – for instance, your hw program graders
i. Actually just refers to a memory location – this replaces the identifier after compile
ii. Variable, class, method names, etc
i. Demo with Hello World
i. No – they are just defined outside of our program
ii. How about String? How about int?
1. String is a class – int is something called a primitive. ‘Primitives’ – we’ll discuss more later.
i. Pretty much the same reason we use descriptive variable names
1. Very helpful for your HW graders as well J
ii. Types of comments in Java:
1. //
2. /* */
3. /** */
a. For tools like JavaDoc
iii. When should you use comments?
1. When something is unclear or confusing
2. In general, use ‘as needed’. Don’t need to comment every line, but reasons for initial variable values, complicated equations etc are good places.
3. Will it slow down your program to use comments?
a. No, because they are not compiled into the Java Byte Code