Lecture 4, CS 302-6, September 12

 

  1. Review
    1. Printing – numbers and strings, and different combinations of them
    2. Errors

                                                               i.      Compile-time

                                                             ii.      Run-time

  1. Variables
    1. Variables store values
    2. Analogy to parking spot – has a unique location, and holds something

                                                               i.      Not 100% true, actually – some variables (eg strings) are pointers.  More on this later

    1. Variables must be declared first

                                                               i.      Type is assigned at declaration – eg int, double, etc

                                                             ii.      int number;

    1. Then, variables must be initialized

                                                               i.      Assigned a starting value

                                                             ii.      number=10;

    1. Note – declaration and initialization can be combined into one step

                                                               i.      int number=10;

    1. Must be declared and initialized before we try to do something with them
    2. Declaring and initializing variables are statements – need to have a ;
    3. Football example
  1. Types of variables
    1. Parking spot analogy – different spots for cars, bikes, motorcycles, etc
    2. int

                                                               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

    1. Double

                                                               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

    1. Literals

                                                               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

    1. Valid values – no commas or quotation marks or fractions

                                                               i.      For instance, “1,000 ½” would not be valid

  1. Variable naming
    1. Requirements vs Coding Standards/norms
    2. Must:

                                                               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

    1. Should:

                                                               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

  1. Identifiers and Reserved Words
    1. Identifier – aka variable name, etc.

                                                               i.      Actually just refers to a memory location – this replaces the identifier after compile

                                                             ii.      Variable, class, method names, etc

    1. Reserved words - Words that carry special significance in Java – for instance, double or Class

                                                               i.      Demo with Hello World

    1. How about ‘System’ or ‘out’ – are they reserved?

                                                               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.

    1. Identifiers cannot have the same name as reserved words
  1. Comments
    1. Why use comments?

                                                               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

  1. HW – Finish reading ch. 2