Java Programming Chapter 2, Slides of Java Programming

Java Programming Language Tutorial

Typology: Slides

2019/2020

Uploaded on 11/08/2020

sparkle-dela-fuente
sparkle-dela-fuente 🇵🇭

5

(1)

5 documents

1 / 52

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Programming
Sixth Edition
Chapter 2
Using Data
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34

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.)