Docsity

Java Programming Chapter 2, Slides of Java Programming

Partial preview of the text

Download Java Programming Chapter 2 and more Slides Java Programming in PDF only on Docsity!

Java Programming

Sixth Edition

Chapter 2

Using Data

Objectives

  • (^) Use constants and variables
  • (^) Use integer data type
  • (^) Use the boolean data type
  • (^) Use floating-point data types
  • (^) Use the char data type

Using Constants and Variables

  • (^) Constant
    • (^) Cannot be changed while program is running
  • (^) Literal constant
    • (^) Value taken literally at each use
  • (^) Numeric constant
    • (^) Opposed to a literal constant
  • (^) Unnamed constant
    • (^) No identifier is associated with it

Using Constants and Variables

(cont'd.)

  • (^) Variable
    • (^) Named memory location
    • (^) Use to store value
    • (^) Can hold only one value at a time
    • (^) Value can change
  • (^) Data type
    • (^) Type of data that can be stored
    • (^) How much memory item occupies
    • (^) What types of operations can be performed on data

Using Constants and Variables

(cont'd.)

Declaring Variables

  • (^) Name variables
    • (^) Using naming rules for legal class identifiers
  • (^) Variable declaration
    • (^) Statement that reserves named memory location
    • (^) Includes:
      • (^) Data type
      • (^) Identifier
      • (^) Optional assignment operator and assigned value
      • (^) Ending semicolon

Declaring Variables (cont'd.)

  • (^) Declare multiple variables of same type in separate

statements on different lines

int myAge = 25; int yourAge = 19;

  • (^) Declare variables of different types
    • (^) Must use separate statement for each type

Declaring Named Constants

  • (^) Named constant
    • (^) Should not change during program execution
    • (^) Has data type, name, and value
    • (^) Data type preceded by keyword final
    • (^) Can be assigned a value only once
    • (^) Conventionally given identifiers using all uppercase letters

The Scope of Variables and Constants

  • (^) Scope
    • (^) Area in which it is visible to a program and in which you can refer to it using its simple identifier
  • (^) A variable or constant is in scope from the point it

is declared

  • (^) Until the end of the block of code in which the declaration lies

Pitfall: Forgetting That a Variable

Holds One Value at a Time

  • (^) Each constant can hold only one value
    • (^) For duration of program
  • (^) Switch values of two variables
    • (^) Use third variable

Learning About Integer Data Types

(cont'd.)

Using the boolean Data Type

  • (^) Boolean logic
    • (^) Based on true-or-false comparisons
  • (^) boolean variable
    • (^) Can hold only one of two values
    • (^) true or false

boolean isItPayday = false;

  • (^) Relational operator (comparison operator)
    • (^) Compares two items

Learning About Floating-Point Data

Types

  • (^) Floating-point number
    • (^) Contains decimal positions
  • (^) Floating-point data types
    • (^) float
    • (^) double
  • (^) Significant digits
    • (^) Refers to mathematical accuracy

Learning About Floating-Point Data

Types (cont'd.)