CS100J, Fall 2001 Thurs 9/6 Lecture 3 ------------------------------------------------------------------------------- Announcements: + new? See Overview on www.cs.cornell.edu/Courses/cs100j/2001fa/! + P1 due NOW + future reading for Tues: 3.1, 3.4 + consulting in lab: Labs-->Carpenter + office hours coming! see Staff-->Staff Listing + P2 will be posted tonight, due Tues 9/18 ------------------------------------------------------------------------------- Topics: + programming language + tokens + statements ------------------------------------------------------------------------------- Reminder: Process of Programming + brainstorm, outline, write, rewrite, polish (1) Gather data - figure out abstract and concrete "things" in problem (2) Gather processes - determine behaviors these "things" have (3) Algorithm - try to connect things and behaviors in a sequence of steps/instructions called an ALGORITHM + convert algorithm to code + algorithm in pseudocode (instructions that look like a programming language) - good pseudocode means that algorithm transferable to most languages! - human language-->algorithm-->code ------------------------------------------------------------------------------- Reminder: Computer Language + instructions that a computer will understand + had features of a written language - syntax (grammar) and semantics (meaning) - characters (keyboard symbols) -> tokens (words) -> statements (sentences) ------------------------------------------------------------------------------- Characters + usually keyboard characters (ASCII text) + ASCII has 128 characters + some are nonprinting -- see ascii.txt for full list + also, try entering $char(0:127)$ in MATLAB (do not enter $s: I use the $ notation to indicate a code fragment or statement to enter) + technically, Java uses UNICODE - over 65000 characters - see p88, Appendix 3, website: Material->Java->Miscellaneous->UNICODE - special non-printing characters can be reproduced with ESCAPE CHARACTERS o see pp86-88 o ex) \n (newline) ------------------------------------------------------------------------------- Tokens + characters build tokens: comments, white space, punctuation, identifiers, operators, literals, strings, reserved words, escape characters + tokens form statements + tokens include: numbers, operators, variables, keywords, punctuation, token separators, strings, comments ------------------------------------------------------------------------------- Token: White Space + blanks, tabs ignored by Java compiler o so, use as many as you want o how much you use usually dictated by style + do NOT separate another token, thoguh o Java will treat as different tokens! o ex) i_am_a_token (the undersore _ is not a blank!) this is a collection of tokens (a blank space is a blank) ------------------------------------------------------------------------------- Token: Comments (pp99-100): + inert + may place anywhere in code + compiler ignores + single line: // I am a comment /* I am a comment */ + multiline: /* blah blah blah blah blah blah */ /* * This version is more aestically pleasing */ + nesting (comment within a comment) o cannot have /* /* stuff */ */ o can nest // comments inside /* */ + lots of style issues to address o generally, you must comment your code o for now, copy style the instructors/text uses ------------------------------------------------------------------------------- Token: Identifiers (p29, chap 2): + names for things in code + store values, act as commands you activate + begin with letter or underscore (_) + may also begin with currency symbols ($ and others) but NOT recommended + may not begin with a number + use as many numbers,letters,and _ inside an indentifier as you wish + CASE SENSITIVE!!! ex) value1 I_am_an_identifier I am five differnent identifiers ------------------------------------------------------------------------------- Token: Reserved words (sometimes called keywords): + part of the language + not available for use as identifiers (where users redefine) ex) public, class, void ------------------------------------------------------------------------------- Token: Primitive data: + boolean, char, byte, short, int, long, float, double + the values are also called CONSTANTS or LITERALS + one "solid" value (not composite) ex) true --> boolean, also a reserved word (same with false) 'a' --> char (character -> single ASCII character) 123 --> int (integer -> whole number) 1.23 --> double (decimal number) + we'll use boolean, char, int, double and skip the rest ------------------------------------------------------------------------------- Token: Strings + collections of characters + different kind of token: sometimes referred to as string literal + ex) "I am a string" + looks like a constant and seems to be a value + actually, really an object, but we're not dealing with objects yet ------------------------------------------------------------------------------- Token: Operators: (Appendix 2 for full list) - +-=,*% (and more) ------------------------------------------------------------------------------- Token: Punctuation: ; () {} separate tokens, statements ------------------------------------------------------------------------------- Token: example of tokens at work: System.out.println(1+19); + tells Java to print out value of 1+19 + name(stuff) is a METHOD CALL (also invocation) + first, what's a statement? (we'll deal with methods later) =============================================================================== Statements: + combine tokens to create "sentences" + each "sentence" forms an instruction/command + "sentences" are called statements + end statements with semicolon + going to a new line does NOT mean you have a new statement! + Java executes each statement top-down, left-to-right (within a statement, operator precedence determines direction of execution) + see Java in Nutshell pp43-44 for full list ------------------------------------------------------------------------------- Statement: Empty + doesn't effectively do anything ex) ; (Java succeeds and moves to next statement) ------------------------------------------------------------------------------- Statement: Expression + combine constants with operators + precedence: some operators "come before" others + associativity: operators "work" left to right or right to left ex) 1 + 1; 2 * 2 - 4; // technically means (2*2) - 4 because of precedence 2 - 1 - 3; // technically means (2-1) - 3 due to associativity + use () to force some expressions to evaluate first ex) (1 + 1); // () not needed 2 * (2 - 4); 2 - (1 - 3); + beware of mixing types! ex) System.out.println(1/2) yields 0, not 0.5 + mixing doubles and ints makes doubles ex) System.out.println(1.0/2) yields 0.5 + use CAST to force different value syntax: (type) expression ex) System.out.println( (int) 9.8) yields 9 (an integer) ------------------------------------------------------------------------------- Statement: Declaration: + want identifiers to represent other values + variables (identifiers) can hold values + values can be primitive types + can also be references, but that deals with objects, so we'll skip this for now + Java is strongly typed, so must tell Java about variables before using syntax: variabletype var ; ex) char c; int value; ------------------------------------------------------------------------------- Statement: Assignment + special kind of expression statement + store value in a variable that's been declared + cannot use variable until it's declared! + syntax: declaredvar = value + the value's type MUST match the variable's type ex) int x; x = 1; + now, x has the value 1 + think x <- 1 because assign operator associates right-to-left + can say in English "x gets 1" + later in code, Java "knows" x means 1 + cannot redeclare variable unless "elsewhere" in code (too hard to explain for now -- so, just don't redeclare!) + shortcut: o can combine syntax for shortcut: o syntax: type var = value o ex) int y = 7; o called INITIALIZING a variable (declaring and assigning initial value) ------------------------------------------------------------------------------- Statement: Method Call + something1(something2) has the syntax name(expression) + think function --> Java will do whatever something1 instructs ex) System.out.println("Hello"); ------------------------------------------------------------------------------- Statement: Style issues + usually one statement per line + leave space between operators and operands (values operated upon) + use descriptive variable names + avoid hard coding values (using specific values that) o use named constants o variables initialized "early" in code and used instead of the constants -------------------------------------------------------------------------------