Chapter 3 Syntax, Errors, and Debugging - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Chapter 3 Syntax, Errors, and Debugging

Description:

Construct and use numeric and string literals. Name and use variables and constants. ... Understand the precedence of different arithmetic operators. ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 35
Provided by: serv01
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Syntax, Errors, and Debugging


1
Chapter 3Syntax, Errors, and Debugging
  • Fundamentals of Java

2
Objectives
  • Construct and use numeric and string literals.
  • Name and use variables and constants.
  • Create arithmetic expressions.
  • Understand the precedence of different arithmetic
    operators.
  • Concatenate two strings or a number and a string.

3
Objectives (cont.)
  • Know how and when to use comments in a program.
  • Tell the difference between syntax errors,
    run-time errors, and logic errors.
  • Insert output statements to debug a program.

4
Vocabulary
  • Arithmetic expression
  • Comments
  • Literal
  • Logic Error
  • Package

5
Vocabulary (cont.)
  • Pseudocode
  • Reserved words
  • Run-time error
  • Syntax
  • Virus

6
Language Elements
  • Every language, including Java has
  • Vocabulary Set of all of the words and symbols
    in the language
  • Syntax Rules for combining words into sentences
    (statements)
  • Semantics Rules for interpreting the meaning of
    statements

7
Language Elements (cont.)
Table 3-1 Some Java vocabulary
8
Language Elements (cont.)
  • Programming vs. natural languages
  • Programming languages have small vocabularies and
    simple syntax and semantics.
  • Programming language syntax must be absolutely
    correct.
  • Programming language statements are interpreted
    literally.
  • Every detail must be present.

9
Basic Java Syntax and Semantics
  • Two categories of data types
  • 1. Primitive data types Numbers, characters, and
    Booleans
  • 2. Objects
  • Syntax for manipulating primitive data types
    differs than for objects
  • Primitive data types are combined in expressions
    with operators.
  • Objects are sent messages.

10
Basic Java Syntax and Semantics (cont.)
  • Objects must be instantiated before use.
  • Unlike primitives
  • String objects are a little different.
  • Six numeric data types
  • int and double are most commonly used
  • Also short, long, byte, and float
  • Each uses a different number of bytes for
    storage.
  • Each represents a different range of values.

11
Basic Java Syntax and Semantics (cont.)
Table 3-2 Some Java numeric data types
12
Basic Java Syntax and Semantics (cont.)
  • Literals Items whose values do not change.
  • The number 5.0 or the string Java
  • Variable is a named location in memory.
  • Changing a variables value is equivalent to
    replacing the value at the memory location.
  • A variables data type cannot change.

13
Basic Java Syntax and Semantics (cont.)
Figure 3-1 Changing the value of a variable
14
Basic Java Syntax and Semantics (cont.)
  • Variable declaration statement Declares the
    identifier and data type for a variable
  • int age (declares one int variable)
  • int a, b, c (declares three int variables)
  • double d 2.45 (declares and initializes a
    variable)
  • Constants are variables whose value cannot
    change.
  • final double PI 3.14

15
Basic Java Syntax and Semantics (cont.)
  • Assignment statements
  • ltvariablegt ltexpressiongt
  • Value of expression assigned to variable
  • Arithmetic expressions
  • Multiplication and division have higher
    precedence than addition and subtraction.
  • Operators of same precedence evaluated from left
    to right.
  • Parentheses are used to change evaluation order.

16
Basic Java Syntax and Semantics (cont.)
Table 3-5 Common operators and their precedence
17
Basic Java Syntax and Semantics (cont.)
  • The semantics of division (/) differ for integers
    and floating-point operators.
  • int / int yields an int.
  • double / double yields a double.
  • The modulus operator () yields a remainder.
  • 11 3 yields 2.

18
Basic Java Syntax and Semantics (cont.)
Table 3-6 Examples of expressions and their
values
19
Basic Java Syntax and Semantics (cont.)
  • Arithmetic overflow Assigning a value to a
    variable that is outside of the ranges of values
    that the data type can represent
  • Mixed-mode arithmetic Expressions involving
    integer and floating-point values
  • Lower-precision data types (int) temporarily
    converted to high-precision data types (double)

20
Basic Java Syntax and Semantics (cont.)
  • Type casting Temporarily converting one data
    type to another
  • Can type cast a single variable or an entire
    expression
  • Place the desired data type within parentheses
    before the variable or expression that will be
    cast to another data type.
  • int x (int)(d 1.6)

21
Basic Java Syntax and Semantics (cont.)
  • String concatenation Append a String or value to
    another String
  • Use the operator
  • String s string1 string2
  • String s2 String1 intVariable1
  • Escape character (\) Used in codes to represent
    characters that cannot be directly typed into a
    program
  • \t is a tab character

22
Basic Java Syntax and Semantics (cont.)
  • The String classs length method gives the number
    of characters in a String.
  • Classes implement methods, and objects are
    instances of classes.
  • Objects can respond to a message only if their
    class implements the method.
  • Must implement a method with a matching signature

23
Basic Java Syntax and Semantics (cont.)
  • Method signature
  • Method name
  • Number and data types of method parameters
  • Method and variable names are user defined
    symbols.
  • Cannot use Java keywords (reserved words)
  • Packages Used to organize related classes into a
    single unit for distribution

24
Basic Java Syntax and Semantics (cont.)
Table 3-7 Javas reserved words
25
Terminal I/O for Different Data Types
Table 3-8 Methods in class Scanner
26
Terminal I/O for Different Data Types (cont.)
Example 3-1 Tests three types of input data
27
Comments
  • Explanatory sentences inserted in a program
  • Compiler ignores them
  • Purpose is to make program more readable
  • Two varieties
  • End of line comments All text following a double
    slash (//) on a single line
  • Multiline comments All text occurring between a
    / and a /

28
Comments (cont.)
  • Typical uses of comments
  • Begin a program with a statement of its purpose
  • Explain the purpose of a variable declaration
  • Explain the purpose of a major segment of code
  • Explain the workings of complex or tricky
    sections of code

29
Programming Errors
  • Three types of programming errors
  • Syntax errors When a syntax rule is violated
  • Detected during compilation
  • Compiler helps identify error
  • Run-time errors Occurs during execution
  • Dividing by 0
  • Detected when program runs
  • JVM indicates type of error and location

30
Programming Errors (cont.)
  • Three types of programming errors (cont.)
  • Logic errors (design errors or bugs) Incorrect
    logic implemented in the program
  • Code may be correct in every other way, but does
    not do what it is supposed to do.
  • Must thoroughly test and debug the program when
    an error is found.
  • Desk checking Examine code immediately after it
    is written

31
Debugging
  • One debugging method is to add extra lines of
    code to print values of selected variables at
    strategic points in the program.

32
Summary
  • Use the int data type for whole numbers and
    double for floating-point numbers.
  • Variable and method names consist of a letter
    followed by additional letters or digits.
  • Keywords cannot be used as names.
  • Final variables behave as constants their values
    cannot change after they are declared.

33
Summary (cont.)
  • Arithmetic expressions are evaluated according to
    precedence.
  • Some expressions yield different results for
    integer and floating-point operands.
  • Strings may be concatenated.
  • The compiler catches syntax errors.
  • The JVM catches run-time errors.

34
Summary (cont.)
  • Logic errors, if caught, are detected by the
    programmer or user at run-time.
  • Can find and remove logic errors by inserting
    debugging output statements to view the values of
    variables.
  • The programmer can modify the color with which
    images are drawn and the properties of text fonts
    for a given graphics object.
Write a Comment
User Comments (0)
About PowerShow.com