1 / 13

CSCI 1100/1202

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 once. To simplify steps in a piece of code. To help prevent code duplication. See Variables.java. Variables.

hireland
Download Presentation

CSCI 1100/1202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 1100/1202 January 16, 2002

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

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

  4. data type variable name 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

  5. Value of a variable • A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; • 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)

  6. Assignment • An assignment statement changes the value of a variable • The assignment operator is the = sign total = 55; • 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 • You can only assign a value to a variable that is consistent with the variable's declared type • See Geometry.java (page 62)

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

  8. Constants • Give names to otherwise unclear literal values • Facilitate changes to the code • Prevent inadvertent errors • See Variables2.java

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

  10. Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 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 127 32,767 2,147,483,647 > 9 x 1018 Numeric Primitive Data • The difference between the various numeric primitive types is their size, and therefore the values they can store:

  11. Characters • Achar 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'

  12. uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ... 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:

  13. Boolean • Abooleanvalue 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 wordstrueandfalseare the only valid values for a boolean type boolean done = false;

More Related