Sections 3 1 3 2 a Basic Syntax

Sections 3. 1 – 3. 2 a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4 th Edition Lambert / Osborne

Language Elements Chapter 3 l Programming and natural languages share three elements. – Vocabulary: Words and symbols – Syntax: Rules for combining statements. l – Semantics: Rules for interpreting statements. l 2 Use of operators, parentheses, etc. Order of precedence

Language Elements (continued) l l Programming Vs. Natural Languages: Programming and natural languages have three differences. – Chapter 3 – l Size: small vocabulary, simple syntax. Rigidity: Syntax must be absolutely correct. Literalness: Computers follow exact instructions. It is difficult to express complex ideas using limited syntax and semantics of programming languages.

Basic Java Syntax and Semantics l l l Chapter 3 l 4 l Data Types: Primitive: numbers, characters, booleans Objects: scanners, strings, and more Syntax: Syntax differs for objects and primitive data. – – Primitive: data types are combined in expressions with operators (addition, multiplication) Objects: sent messages, must be instantiated before use

Basic Java Syntax and Semantics (continued) l l Numbers: Numeric data types: – Chapter 3 – 5 Most programs input numeric data, perform calculations, output numeric results. Java includes six numeric data types but we use two:

Basic Java Syntax and Semantics (continued) l l Literals: Literals are items in a program whose values do not change. Restricted to primitive data types and strings. Chapter 3 – Examples of numeric literals 6

Basic Java Syntax and Semantics (continued) l Chapter 3 l Variables and Their Declarations: Variables are items whose values can change during execution. 7 Changing the value of a variable

Basic Java Syntax and Semantics (continued) l l Variables and Their Declarations (cont): Before using a variable, the program must declare its type. – Variable declaration statement Type on left; variable name on right – Several variables and values can be in the same statement. Chapter 3 – 8

Basic Java Syntax and Semantics (continued) l Chapter 3 l 9 l Variables and Their Declarations (cont): Instantiation creates objects. Constants are variables that, once initialized, cannot change.

10 Chapter 3
- Slides: 10