csci 1100/1202 january 16, 2002. why do we need variables? to store intermediate results in a long...

13
CSCI 1100/1202 January 16, 2002

Upload: pierce-stewart

Post on 27-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

CSCI 1100/1202

January 16, 2002

Page 2: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Why do we need variables?• To store intermediate results in a long

computation.

• To store a value that is used more than once.

• To simplify steps in a piece of code.

• To help prevent code duplication.

• See Variables.java

Page 3: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Variables

• A variable is a name for a location in memory

• Can be used to store a value for later use.

• Can hold a value of a specific type.

• Can be modified at any time in the program.

• Must be declared before using.

• Uses a unique identifier for a name.

Page 4: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Variable Declaration

• A variable must be declared, specifying the variable's name and the type of information that will be held in it

int total;

int count, temp, result;

Multiple variables can be created in one declaration, but it is generally a better practice to put one variable per line with an appropriate comment

data type variable name

Page 5: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Value of a variable

• A variable can be given an initial value in the declaration

• Once assigned a value, the variable name can be used in any expression.

• When evaluated, the current value of the variable is substituted for that variable name.

• See PianoKeys.java (page 60)

int sum = 0;int base = 32, max = 149;

Page 6: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Assignment• An assignment statement changes the value of a

variable• The assignment operator is the = sign

total = 55;

• You can only assign a value to a variable that is consistent with the variable's declared type

• The expression on the right is evaluated and the result is stored in the variable on the left

• The value that was in total is overwritten

• See Geometry.java (page 62)

Page 7: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Constants• A constant is an identifier that is similar to

a variable except that it holds one value for its entire existence

• The compiler will issue an error if you try to change a constant

• In Java, we use the final modifier to declare a constant

final int MIN_HEIGHT = 72;

Page 8: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Constants• Give names to otherwise unclear literal

values

• Facilitate changes to the code

• Prevent inadvertent errors

• See Variables2.java

Page 9: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Primitive Data

• There are exactly eight primitive data types in Java

• Four of them represent integers:– byte, short, int, long

• Two of them represent floating point numbers:– float, double

• One of them represents characters:– char

• And one of them represents boolean values:– boolean

Page 10: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Numeric Primitive Data

• The difference between the various numeric primitive types is their size, and therefore the values they can store:

Type

byteshortintlong

floatdouble

Storage

8 bits16 bits32 bits64 bits

32 bits64 bits

Min Value

-128-32,768-2,147,483,648< -9 x 1018

+/- 3.4 x 1038 with 7 significant digits+/- 1.7 x 10308 with 15 significant digits

Max Value

12732,7672,147,483,647> 9 x 1018

Page 11: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Characters

• A char variable stores a single character from the Unicode character set

• A character set is an ordered list of characters, and each character corresponds to a unique number

• The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters

• It is an international character set, containing symbols and characters from many world languages

• Character literals are delimited by single quotes:'a' 'X' '7' '$' ',' '\n'

Page 12: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Characters

• The ASCII character set is older and smaller than Unicode, but is still quite popular

• The ASCII characters are a subset of the Unicode character set, including:

uppercase letterslowercase letterspunctuationdigitsspecial symbolscontrol characters

A, B, C, …a, b, c, …period, semi-colon, …0, 1, 2, …&, |, \, …carriage return, tab, ...

Page 13: CSCI 1100/1202 January 16, 2002. Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than

Boolean

• A boolean value represents a true or false condition

• A boolean can also be used to represent any two states, such as a light bulb being on or off

• The reserved words true and false are the only valid values for a boolean type

boolean done = false;