Lexicon

import java.lang.System; EECS1022 MOBILE COMPUTING public class Area { public static void main(String[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } PROF. Y. LESPÉRANCE } Dept. of Electrical Engineering & Computer Science 1 2

import java.lang.System; import java.lang.System;

public class Area public class Area { Imports { public static void main(String[] args) public static void main(String[] args) { { int width; int width; width = 8; width = 8; int height = 3; int height = 3; int area = width * height; int area = width * height; System.out.println(area); System.out.println(area); } } } } Imported Class = Delegaon 3 4

Java/Lespérance 1 Lexicon

import java.lang.System; import java.lang.*; Class Header Method Header public class Area public class Area { { public static void main(String[] args) public static void main(String[] args) { { int width; int width; Class Body, a Method Body, a width = 8; Block width = 8; int height = 3; int height = 3; Block int area = width * height; int area = width * height; System.out.println(area); System.out.println(area); } } } }

5 6

Style Lexical Elements

Class naming convenon Without worrying about syntax or semancs, let us A noun. Use Pascal/Title case, e.g. Math, ArrayList. idenfy the lexical elements of a program: Method naming convenon A verb. Use camel case, e.g. equals, toString, isLeapYear Keywords Idenfiers Variable naming convenon A noun. Use camel case, e.g. length, interestRate, gender Literals Applies also to aributes and parameters. Operators Block layout Separators Braces must align vercally and the all statements must be le jusfied and indented by one tab posion.

7 8

Java/Lespérance 2 Lexicon

Keywords The reserved words are the keywords plus the literals: abstract assert true, false, null Keywords boolean break byte case catch char class const continue Must not be a reserved word, must begin with a letter, default do double and its character set must be: {0-9, A-Z, a-z, , _} Identifiers else enum extends final finally float for Recognized by the presence of a number, 'character', goto Literals "characters", or one of: true, false, null if implements import instanceof int interface long Operators The character set of operators: native new

= > < ! ~ ? : & | + - * / ^ % package private protected public return Separators short static strictfp super switch synchronized The separators are: this throw throws transient try

. , ; … ( ) [ ] { } void volatile while 9 10

import java.lang.System; Example public class Area { Idenfy the language elements in the public static void main(String[] args) following program... { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); } } Keywords, Identifiers, Literals, Operators, Separators Keywords, Identifiers, Literals, Operators, Separators

11 12

Java/Lespérance 3 Lexicon

import java.lang.System; import java.lang.System;

public class Area public class Area { { public static void main(String[] args) public static void main(String[] args) { { int width; int width; width = 8; width = 8; int height = 3; int height = 3; int area = width * height; int area = width * height; System.out.println(area); System.out.println(area); } } } } Keywords, Identifiers, Literals, Operators, Separators Keywords, Identifiers, Literals, Operators, Separators 13 14

Compile Time vs Run Time Errors Compile Time vs Run Time Errors

• Before program can run, it must be compiled • When you execute your program, you may to (transalted) Java bytecode get runme errors:

• Studio does this as you enter/edit your code; •ArithmeticException , e.g. 10 / 0 it flags compile-me errors: •ArrayIndexOutOfBoundException , etc. •Syntax errors , e.g. missing ; { ( • Logic errors: program appears to run •Type errors , e.g. “abc” * 3 normally but does not behave as required

15 16

Java/Lespérance 4 Declaraon

The Declaraon Statement EECS1022 MOBILE COMPUTING type name;

The name of a primive or non- A separator primive type, e.g. int, double…

EECS4413/ An idenfier to be associated with a memory block Roumani

• The scope of the variable = the enclosing block of the declaraon. PROF. Y. LESPÉRANCE • The variable is not known outside its scope. • Declaraon does not inialize. Not with 0 or null or anything else. Dept. of Electrical Engineering & Computer Science 1 2

Primive & Non-Primive

int 4 ±2G exact number Integer character Primitive long 8 ±2E exact

boolean Integer literals are int by default unless suffixed with L

float 4 ±1038 SD=7 class Real Non-Primitive interface double 8 ±10308 SD=15 Real literals are recognized by a decimal or an exponent. They are array double by default unless suffixed with F. For exponenal notaon, use E.

3 4

Java/Lespérance 1 Declaraon

The Type boolean

Use for integer data, • Stores the result on a condion e.g. count. Integer 100% exact • Has only two possible values • true and false are reserved words • Boolean variables are not integers Use for real data, e.g. amount. Real Inherently inaccurate • The Boolean operators are: ! (for not), && (for and), || (for or), and ^ (for xor)

Note: Boolean literals are the easiest to recognize!

5 6

The Character Type char More on Characters

• A leer, digit, or symbol Code Character Escape Meaning 0 The character whose code is . \uxxxx • Digits versus Numbers . (hex) xxxx 32 space • Store the code, not the . \' Single quote . . \" Double quote • The case of English: ASCII vs UniCode 48-57 '0'-'9' . \\ Backslash . • char is thus an (unsigned) integer type New line 65-90 'A'-'Z' \n . \r Carriage return • No char operators! They auto-promote to int. . 97-122 'a'-'z' \f Form Feed . Character literals are recognized by single quotes surrounding the character, e.g. 'A' . \t Tab 65535 \b Backspace

7 8

Java/Lespérance 2 Declaraon

Java’s Primitive Types (in java.lang) PRIMITIVE Size Approximate Range Class Type String Type S.D. TYPES (bytes) min max

I S byte 1 -128 +127 N/A? • Stores a sequence of characters N I short 2 -32,768 +32,767 ? T G N/A • Opmized for speed à immutable N N E int 4 -2×109 +2×109 N/A? E U G • Opmized declaraon à shortcut D 18 18 M E long 8 -9×10 +9×10 N/A? B R UNSIGNED char 2 0 65,535 N/A? • Opmized concatenaon à + operator E R R SINGLE float 4 +3.4×1038 +3.4×1038 7 • Rich API (e.g. indexOf, charAt, substring) E A 308 308 L DOUBLE double 8 -1.7×10 +1.7×10 15 Note: String literals are surrounded with double quotes and can use the same escape sequences as chars. BOOLEAN boolean 1 true/false N/A 9 10

Class Type Date (in java.ul) Class Type Rectangle (in textbook) • Stores an instance of me

• Captures both date and me • Stores an instance of a rectangle • Captures the height and width as int • Accurate to a millisecond

• Simple API (toString and getTime) • API (getArea and getCircumference)

Note: Like all class types (except for String), Date has no literals and no Like all class types (except for String), it has no literals and no operators. operators.

11 12

Java/Lespérance 3 Declaraon

Class Type Fraction Class Type TextView (in i2c library) (in android.widget)

• Stores an instance of a fracon • Stores a UI label • Numerator and denominator are long • Many aributes: text, layout, style, … • API (add, sub, mulply, divide, …) • 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 A separator - Its type can hold RHS - Its content will be overwri

- a Literal - a Name, or - an Expression

15

Java/Lespérance 4