Lexicon CS1022 MOBIL COMPUTING PROF Y LSPÉRANC Dept of lectrical ngineering & Computer Science 1 2 Imports 3 Imported Class = DelegaKon 4 Java/Lespérance 1
Lexicon Class Header Class Body, a Block import javalang*; Method Header Method Body, a Block 5 6 Style Class naming conven:on A noun Use Pascal/Title case, eg Math, ArrayList Method naming conven:on A verb Use camel case, eg equals, tostring, isleapyear Variable naming conven:on A noun Use camel case, eg length, interestrate, gender Applies also to anributes and parameters Block layout Braces must align ver:cally and the all statements must be leh jus:fied and indented by one tab posi:on Lexical lements Without worrying about syntax or semankcs, let us idenkfy the elements of a program: Keywords Iden:fiers Literals Operators Separators 7 8 Java/Lespérance 2
Lexicon Keywords Identifiers Literals Operators Separators The reserved words are the keywords plus the literals: true, false, null Must not be a reserved word, must begin with a letter, and its character set must be: 0-9, A-Z, a-z,, _ Recognized by the presence of a number, 'character', "characters", or one of: true, false, null The character set of operators: = > <! ~? : & + - * / ^ % The separators are:, ; ( ) [ ] 9 Keywords abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while 10 xample IdenKfy the language elements in the following program Keywords, Identifiers, Literals, Operators, Separators Keywords, Identifiers, Literals, Operators, Separators 11 12 Java/Lespérance 3
Lexicon Keywords, Identifiers, Literals, Operators, Separators 13 Keywords, Identifiers, Literals, Operators, Separators 14 Compile Time vs Run Time rrors Before program can run, it must be compiled to (transalted) Java bytecode Studio does this as you enter/edit your code; it flags compile-:me errors: Syntax errors, eg missing ; ( Type errors, eg abc * 3 Compile Time vs Run Time rrors When you execute your program, you may get run:me errors: Arithmeticxception, eg 10 / 0 ArrayIndexOutOfBoundxception, etc Logic errors: program appears to run normally but does not behave as required 15 16 Java/Lespérance 4
Declara'on CS1022 MOBIL COMPUTING The Declara7on Statement type name; The name of a primi7ve or nonprimi7ve type, eg int, double A separator CS4413/ Roumani An iden7fier to be associated with a memory block PROF Y LSPÉRANC Dept of lectrical ngineering & Computer Science The scope of the variable = the enclosing block of the declara6on The variable is not known outside its scope Declara6on does not ini6alize Not with 0 or null or anything else 1 2 Primi7ve & Non-Primi7ve Primitive number character Integer int long 4 8 ±2G exact ±2 exact Non-Primitive boolean class interface array Integer literals are int by default unless suffixed with L Real float double 4 8 ±10 38 SD=7 ±10 308 SD=15 Real literals are recognized by a decimal point or an exponent They are double by default unless suffixed with F For exponen'al nota'on, use 3 4 Java/Lespérance 1
Declara'on The Type boolean Integer Real Use for integer data, eg count 100% exact Use for real data, eg amount Inherently inaccurate Stores the result on a condi7on Has only two possible values true and false are reserved words Boolean variables are not integers The Boolean operators are: && (for and), (for or), and ^ (for xor) Note: Boolean literals are the easiest to recognize! 5 6 The Character Type char A lewer, digit, or symbol Digits versus Numbers Store the code, not the typeface The case of nglish: ASCII vs UniCode char is thus an (unsigned) integer type No char operators! They auto-promote to int Character literals are recognized by single quotes surrounding the character, eg 'A' More on Characters Code Character 0 32 space 48-57 '0'-'9' 65-90 'A'-'Z' 97-122 'a'-'z' 65535 scape \uxxxx \' Meaning The character whose code is (hex) xxxx Single quote \" Double quote \\ Backslash \n New line \r Carriage return \f Form Feed \t Tab \b Backspace 7 8 Java/Lespérance 2
Declara'on N U M B R Java s Primitive Type PRIMITIV TYPS I N T G R Type Size (bytes) Approximate Range min max SD S byte 1-128 +127 N/A? I G short 2-32,768 +32,767 N/A? N int 4-2 10 9 +2 10 9 N/A? D long 8-9 10 18 +9 10 18 N/A? UNSIGND char 2 0 65,535 N/A? R SINGL float 4 +34 10 38 +34 10 38 7 A L DOUBL double 8-17 10 308 +17 10 308 15 BOOLAN boolean 1 true/false N/A 9 Class Type String Stores a sequence of characters Op7mized for speed à immutable Op7mized declara7on à shortcut (in javalang) Op7mized concatena7on à + operator Rich API (eg indexof, charat, substring) Note: String literals are surrounded with double quotes and can use the same escape sequences as chars 10 Class Type Date Stores an instance of 7me Captures both date and 7me Accurate to a millisecond Simple API (tostring and gettime) (in javau6l) Class Type RectangleModel (in our package) Stores an instance of a rectangle Captures the height and width as int API (getarea and getcircumference) Note: Like all class types (except for String), Date has no literals and no operators Like all class types (except for String), it has no literals and no operators 11 12 Java/Lespérance 3
Declara'on Class Type FractionModel (in our package) Stores an instance of a frac7on Numerator and denominator are long API (add, sub, mul7ply, divide, ) Class Type TextView (in androidwidget) Stores a UI label Many awributes: text, layout, style, API (gettext, settext, settypeface, ) Like all class types (except for String), it has no literals and no operators Like all class types (except for String), it has no literals and no operators 13 14 An operator name = value; - Pre-declared and in-scope - Its type can hold RHS - Its content will be overwriwen A separator - a Literal - a Name, or - an xpression 15 Java/Lespérance 4