EECS1710 Click to edit Master Week text 01, styles Lecture 02 Second level Third level Fourth level Fifth level Fall 2014! Thursday, Sept 11, 2014 1 Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) warning!! it can take up to 24 hours to get the account. Plan ahead. " read sections 1.1-1.2. " review Ch 1 Key Concepts (KC) 1-13 " do Review Questions (RQ) 1-17 " do Exercises (Ex) 1.1-1.16 " get the Lecture 02 class materials (by Wed noon) 2 1
Checklist for this lecture (Sept 11, 2014) By the end of this lecture, you should " be able to identify the components of a program ( 1.1.1) " being able to recognize the significance of coding style " be able to identify the language elements within a program ( 1.1.2) " be able to describe the edit-compile-run process ( 1.1.3) " distinguish between a java file and a class file, distinguish between byte code and machine code, understand how all of this relates to the architecture of the computer platform 3 Checklist for this lecture (Sept 11, 2014) By the end of this lecture, you should " be able to construct a declaration statement ( 1.2.1) " be able to list Java s primitive types and describe the key issues in data representation ( 1.2.2, 1.2.4) " understand some of the significant differences between primitive and non-primitive values ( 1.2.3, 1.2.5) 4 2
Identifying Program Components Identify the preamble, class header, class body within this simple program. import java.util.date; public class Lect02Ex01 { public static void main(string[] args) { int thevalue = 89; // this app doesn't do very much // it declares a variable, assigns a value, and then stops! 5 Identifying Program Components Identify the preamble, class header, class body within this simple program. import java.io.printstream; public class Area {public static void main(string[] args) {PrintStream output;output = System. out;int width;width = 8;int height = 3;int area = width * height;output.println(area); 6 3
Identifying Program Components Identify the preamble, class header, class body within this simple program. Identify the method header, method body within the class body. package samplepkg; import java.io.printstream; public class Lect02Ex02 { public static void main(string[] args) { int thevalue = 89; // this app doesn't do very much // it declares a variable, assigns a value, and then stops! 7 Anatomy of a Java class Identify the method header, method body within the class body. package samplepkg; import java.io.printstream; public class Lect02Ex03 { public static void main(string[] args) { int thevalue = 89; // this app doesn't do very much // it declares a variable, assigns a value, and then stops! public void mymethod() { int someothervalue = 9; 8 4
The concept of scope SUPER VERY IMPORTANT 1.2, p.16 Notice that the identified thevalue occurs twice. How many variables are being declared and what is the scope of use? package samplepkg; public class Lect02Ex04 { public static void main(string[] args) { int thevalue = 89; public void mymethod() { int thevalue = 90; 9 Anatomy of Java RQ1. What is a package? How is a package different from a subpackage? 10 5
Illustration Here is how a package appears on the file system. 11 Anatomy of Java: Statements, Classify all statements in terms of (1) declaration, (2) assignment, combo declaration/assignment, (3) usage of other classes, (4) flow control, (5) other import java.lang.system; public class Area { public static void main(string[] args) { int width; width = 8; int height = 3; int area = width * height; System.out.println(area); JD 1.3 (p. 10) 12 6
Anatomy of Java: Variable Declaration RQ 6. What does strongly typed mean? Java is strongly typed. Other languages, such as Perl, are weakly typed. Strongly typed means: Weakly typed means: the identifier must be declared as a variable, but the type of the variable need not be declared in advance. Java enforces strong typing discipline so that the compiler can do a lot of checking before producing bytecode. 13 This results in code that has certain safety features (see sec 2.3.1) Anatomy of Java: Variable Declaration Why strong typing? the compiler can check and enforce type compatibility (for instance, if type compatibility is violated, the compiler will issue an error instead of producing bytecode) This results in code that has certain safety features (more on this in sec 2.3.1) 14 7
Lexical Elements Ex 1.7 (riff) In the statements below, identify each of the lexical elements: keywords, identifiers, literals, operators, separators. double balance = 34e3; boolean isvalid = false; long count = System.out.readLong(); booleanisvalid = `F`; String address = new String( Toronto ); number = Integer.parseInt(input); public static void main(string[] commmand) System.out.printf( %,.2f, amount) 15 Lexical Elements RQ.10. How do you know whether a word is a Java reserved word? RQ 13. Can you determine the type of each literal below? 34e3 false `F` Toronto 34.6 1L 2f 3e6f 16 16 8
Lexical Elements Analyse these tokens. Which tokens are literals and which are not? False `FX` 34.6 1.9L 1.233333333333333333333333333333333333399999999999 99999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999 9999999999999999f 17 Edit-Compile-Run RQ 14. What is the difference between a source and a class file? 18 9
Syntax (and Semantic) Rules RQ.11. What are the syntactical rules for naming identifiers? ***What are the semantic rules for naming identifiers? 19 Coding Style RQ.12. What are the style guidelines for naming identifiers? What sorts of identifiers are there? 20 10
Primitive Types RQ.17. Name Java s primitive types. Why are they called primitive? Are there literal values for every primitive type in Java? 21 11