Variables, Types, Values 14 January 2019 OSU CSE 1 Variables A - - PowerPoint PPT Presentation

variables types values
SMART_READER_LITE
LIVE PREVIEW

Variables, Types, Values 14 January 2019 OSU CSE 1 Variables A - - PowerPoint PPT Presentation

Variables, Types, Values 14 January 2019 OSU CSE 1 Variables A variable is the name of a location that stores a value of a particular type We might say the variable has that value We might say the variable


slide-1
SLIDE 1

Variables, Types, Values

14 January 2019 OSU CSE 1

slide-2
SLIDE 2

Variables

  • A variable is the name of a “location” that

“stores” a value of a particular type

– We might say the variable “has” that value – We might say the variable “has” that type or “is of” that type

14 January 2019 OSU CSE 2

slide-3
SLIDE 3

Examples

14 January 2019 OSU CSE 3

true 'y' 13 3.14 "Go"

slide-4
SLIDE 4

Examples

14 January 2019 OSU CSE 4

This is a boolean variable b whose value is true, i.e., b = true

  • r, more simply, just

b true 'y' 13 3.14 "Go"

slide-5
SLIDE 5

Examples

14 January 2019 OSU CSE 5

This is a char variable c whose value is 'y', i.e., c = 'y' true 'y' 13 3.14 "Go"

slide-6
SLIDE 6

Examples

14 January 2019 OSU CSE 6

This is a int variable i whose value is 13, i.e., i = 13 true 'y' 13 3.14 "Go"

slide-7
SLIDE 7

Examples

14 January 2019 OSU CSE 7

This is a double variable d whose value is 3.14, i.e., d = 3.14 true 'y' 13 3.14 "Go"

slide-8
SLIDE 8

Examples

14 January 2019 OSU CSE 8

This is a String variable s whose value is "Go", i.e., s = "Go" (or s = <'G', 'o'>) true 'y' 13 3.14 "Go"

slide-9
SLIDE 9

Types

  • A type is the name of the set of all

possible values that a variable might have

  • Examples:

– A variable of type String might have values like "foo", "Hello World", etc. – A variable of type int might have values like –1, 18, etc. – A variable of type double might have values like 3.1416, 10.0, etc.

14 January 2019 OSU CSE 9

slide-10
SLIDE 10

Program vs. Mathematical Variables

  • A program variable has a particular value

at any one time during program execution, and that value (generally) may change at

  • ther times
  • A mathematical variable stands for an

arbitrary but fixed value

14 January 2019 OSU CSE 10

slide-11
SLIDE 11

Program vs. Mathematical Types

  • A program type has a corresponding

mathematical type that models it

14 January 2019 OSU CSE 11

slide-12
SLIDE 12

Program vs. Mathematical Types

  • A program type has a corresponding

mathematical type that models it

14 January 2019 OSU CSE 12

When reasoning about a program variable of a given program type, treat its value at any given time as if it were a mathematical variable of the corresponding mathematical type.

slide-13
SLIDE 13

Mathematical Models

Program type Mathematical type String string of character boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits)

14 January 2019 OSU CSE 13

slide-14
SLIDE 14

Mathematical Models

Program type Mathematical type String string of character boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits)

14 January 2019 OSU CSE 14

String is built-in to Java; boolean, char, int, and double are among the 8 primitive (and also built-in) types of Java; differences later.

slide-15
SLIDE 15

Mathematical Models

Program type Mathematical type String string of character boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits)

14 January 2019 OSU CSE 15

All these mathematical types are “built-in” to mathematics!

slide-16
SLIDE 16

Mathematical Models

Program type Mathematical type String string of character boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits)

14 January 2019 OSU CSE 16

Program code is shown in a blue fixed-width font, with keywords in bold.

slide-17
SLIDE 17

Mathematical Models

Program type Mathematical type String string of character boolean boolean char character int integer (-2147483648 through 2147483647) double real (about ±10±308, 15 significant digits)

14 January 2019 OSU CSE 17

Mathematics is shown in a green fixed-width italic font, with keywords in bold.

slide-18
SLIDE 18

Declaring a Variable

  • When you declare a program variable,

you both provide a name for a location to store its value, and indicate its program type

– Recall: the program type determines the mathematical type, which in turn determines the possible values the variable can have

int j;

14 January 2019 OSU CSE 18

?

slide-19
SLIDE 19

Declaring a Variable

  • When you declare a program variable,

you both provide a name for a location to store its value, and indicate its program type

– Recall: the program type determines the mathematical type, which in turn determines the possible values the variable can have

int j;

14 January 2019 OSU CSE 19

? The standard Java convention for naming variables is to use camel case: start with a lower case letter and only capitalize the first letter of each following word, e.g., myLuckyNumber

slide-20
SLIDE 20

Declaring a Variable

  • When you declare a program variable,

you both provide a name for a location to store its value, and indicate its program type

– Recall: the program type determines the mathematical type, which in turn determines the possible values the variable can have

int j;

14 January 2019 OSU CSE 20

This is an int variable j whose value is undefined. ?

slide-21
SLIDE 21

Initializing a Variable

  • To initialize a variable, you assign it a

value

– Recall: the program type determines the mathematical type, which in turn determines the possible values the variable can have

int j = 13;

14 January 2019 OSU CSE 21

13

slide-22
SLIDE 22

Initializing a Variable

  • To initialize a variable, you assign it a

value

– Recall: the program type determines the mathematical type, which in turn determines the possible values the variable can have

int j = 13;

14 January 2019 OSU CSE 22

This is an int variable j whose value is 13, i.e., j = 13 13

slide-23
SLIDE 23

Reasoning: Tracing Tables

14 January 2019 OSU CSE 23

Code State

x = 1.414 int j = 13; x = 1.414 j = 13

slide-24
SLIDE 24

Reasoning: Tracing Tables

14 January 2019 OSU CSE 24

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 Every other row in the left column contains some program statement(s).

slide-25
SLIDE 25

Reasoning: Tracing Tables

14 January 2019 OSU CSE 25

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 Every other row in the right column contains some mathematical sentences ("facts").

slide-26
SLIDE 26

Reasoning: Tracing Tables

14 January 2019 OSU CSE 26

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 This equal sign, in code, means assignment of a value to a program variable.

slide-27
SLIDE 27

Reasoning: Tracing Tables

14 January 2019 OSU CSE 27

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 This equal sign, in mathematics, means equality of two values.

slide-28
SLIDE 28

Reasoning: Tracing Tables

14 January 2019 OSU CSE 28

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 There is no value for mathematical variable j in this state because program variable j hasn’t been declared yet.

slide-29
SLIDE 29

Reasoning: Tracing Tables

14 January 2019 OSU CSE 29

Code State

x = 1.414 int j = 13; x = 1.414 j = 13 There is a value for j in this state because j has been declared before this state.

slide-30
SLIDE 30

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 30

slide-31
SLIDE 31

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 31

This is a String literal; written as characters between double-quote marks: "…"

slide-32
SLIDE 32

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 32

This is a boolean literal; must be either true or false.

slide-33
SLIDE 33

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 33

This is a char literal; normally written as a single character between single-quote marks: '…'

slide-34
SLIDE 34

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 34

This is an int literal; normally written (as in mathematics) as a decimal constant.

slide-35
SLIDE 35

Literals

  • A data value appearing, literally, in a

program is called a literal

String fileName = "foo.txt"; boolean found = false; char win = 'W'; int j = 13; double ht = 9.27;

14 January 2019 OSU CSE 35

This is a double literal; normally written (as in mathematics) as a decimal constant with a decimal point.

slide-36
SLIDE 36

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 36

slide-37
SLIDE 37

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 37

escaped special character: single-quote

slide-38
SLIDE 38

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 38

non-printing character: tab

slide-39
SLIDE 39

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 39

Unicode character: small Greek π

slide-40
SLIDE 40

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 40

  • ctal integer

(base-8): 29 in decimal

slide-41
SLIDE 41

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 41

hexadecimal integer (base-16): 26 in decimal

slide-42
SLIDE 42

Forms of Literals

Program type Literal examples String "I\'m" "at OSU" boolean true false char 'A' '\t' '\"' '\u03c0' int 29

  • 13

035 0x1a double

  • 18. 18.0

8E-4 6.022E23

14 January 2019 OSU CSE 42

scientific notation: 8 x 10–4

slide-43
SLIDE 43

Constants

  • A variable whose value is initialized and

never changed is called a constant

int myLuckyNumber = 13; double avogadro = 6.022E23;

14 January 2019 OSU CSE 43

slide-44
SLIDE 44

Constants

  • A variable whose value is initialized and

never changed is called a constant

final int myLuckyNumber = 13; final double avogadro = 6.022E23;

14 January 2019 OSU CSE 44

The keyword final indicates to the compiler your intent that a variable is actually a constant.

slide-45
SLIDE 45

Constants

  • A variable whose value is initialized and

never changed is called a constant

final int myLuckyNumber = 13; final double avogadro = 6.022E23;

14 January 2019 OSU CSE 45

When constants are declared inside a method, the standard Java convention is to use camel case just like for variables.

slide-46
SLIDE 46

Constants

  • A variable whose value is initialized and

never changed is called a constant

final int MY_LUCKY_NUMBER = 13; final double AVOGADRO = 6.022E23;

14 January 2019 OSU CSE 46

When constants are declared at the class level, the standard Java convention is to use all upper case letters and to separate the words with '_'.

slide-47
SLIDE 47

Resources

  • Java for Everyone, Chapter 2

– http://osu.worldcat.org/title/java-for-everyone-late-objects/oclc/808511232

14 January 2019 OSU CSE 47