Data Types and Variables
An Introduction with Examples in Java
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department |
bernstdh@jmu.edu |
|
Data and Values
- Data:
- A datum (or piece of data) is a thing that is (known
and) used for calculation or reasoning
- Values:
- A value is the representation of a datum
Variables and Constants
- Variable:
- A named space for holding a value
- Constant:
- A named space for holding a value that does
not change
- Atomic Variables/Constants:
- A variable/constant that can hold one value
(e.g., a number or character)
- Identifiers:
- The name of a variable/constant
A Little Background on Digital Computers
- Physical Storage Devices:
- Electronic/Magnetic Systems - usually differentiate between
positive and negative, on and off, or clockwise and
counterclockwise
- Mechanical Systems - usually differentiate between up and down,
pits and lands, holes and solids, or bumps and flats
- An Abstraction:
- Consider two possible values, 0 and 1
Data Representation - The Counting Numbers
Data Representation - Negative Integers
- An Obvious Place to Start:
- Since there are two signs, use one bit (e.g., the left-most)
to represent the sign
- A Shortcoming of this Approach:
- It results in both a +0 and a -0
Going Further
: Negative Numbers
- Ones Complement:
- Use the leftmost bit to indicate sign
- One byte can be used to represent [-127, 127]
- Results in both +0 and -0
- Twos Complement:
- Eliminate the extra 0
- One byte can be used to represent [-128, 127]
- How do you "create" them?
Going Further
: Negative Numbers (cont.)
Going Further
: Negative Numbers (cont.)
(Courtesy of xkcd)
Data Representation - Real Numbers
- Think About Base 10:
- The positions to the left of the decimal point are powers of 10
and the positions to the right of the decimal place are powers
of 1/10
- An Obvious Place to Start in Binary:
- The positions to the left of the decimal point are powers of 2
and the positions to the right of the decimal place are powers
of 1/2
Going Further
: Real Numbers (cont.)
- Terms:
- Normalization:
- One digit left of the decimal
- Example: +1.101101 x 23
- Sign: +
- Exponent: 3
- Mantissa: 1.101101
Going Further
: Real Numbers (cont.)
- IEEE Short Real (Single Precision):
- 1 bit for the sign
- 8 bits for the exponent
- 23 bits for the mantissa
- IEEE Long Real (Double Precision):
- 1 bit for the sign
- 11 bits for the exponent
- 52 bits for the mantissa
Data Representation - Characters
- An Obvious Place to Start:
- Count the number of characters
- Determine the number of bits needed
- Assign a binary number to each character
- An Example:
- There are 26 letters in the alphabet
- 5 bits can represent \(2^5\) (i.e., 32) different things
- Assign 00001 to A, 00010 to B, 00011 to C, ...., 11010 to Z
Data Representation - Characters (cont.)
- The ASCII (American Standard Code for Information Interchange) Encoding:
- Unicode:
- A mapping for every character in every language
(including many dead languages)
Some Atomic/Primitive/Fundamental Types in Java
Type
|
Memory
|
Range
|
byte
|
1 byte |
-128 to 127 |
short
|
2 bytes |
-32,768 to 32,767 |
int
|
4 bytes |
-2,147,483,648 to 2,147,483,647 |
long
|
8 bytes |
\(-2^{63}\) to \(2^{63}-1\) |
float
|
4 bytes |
|
double
|
8 bytes |
|
char
|
2-4 bytes |
Unicode |
boolean
|
|
true or false |
Data Representation - Some Non-Atomic Types
- Text:
- Think of text as consisting of one or more characters
- Colors:
- Think of colors as having a red, green and blue component
- Images:
- Create a finite grid (called a raster) with equal sized
cells (called picture elements or pixels) each of which
contains a color
Non-Atomic Types in Java
- An Observation:
- The number of components of non-atomic types may be unknown,
hence the amount of memory required may be unknown
- The Approach in Java:
- Request the amount of memory needed
and store the address of that memory
- Java Conventions:
- Types of this kind are called reference types
- Types of this kind have names that start with an
uppercase letter
Variables in Java
- Name/Identifier Restrictions:
- Must start with a letter
- Can contain letters, digits, and '_'
- Are case-sensitive
- Course Style Guide Requirements:
- Must start with a lowercase letter
- Must be descriptive
Variables in Java (cont.)
Nerd Humor
(Courtesy of xkcd)
Variable Declarations
- Purposes:
- Set aside enough memory to hold the datum
- Allow the memory that is set aside to be referred to by name
elsewhere (with some limitations) in the program
- Advantages of Typed Declarations:
- The amount of memory to set aside is known
- It is easy to determine if a variable is being used
in a manner that is consistent with its type
Declaration Statements in Java
- Syntax:
-
type
identifier [, identifier]... ;
- Examples:
-
boolean done;
-
double expenses, income;
-
int numberOfChecks, styleNumber;
-
String name;
- Course Style Guide Requirements:
- Each declaration must appear in its own statement (i.e.,
you can not have multiple identifiers separated by commas)
Declaration Statements in Java (cont.)
- Atomic/Primitive/Fundamental Types:
- Enough memory is set aside to hold a value of that type
(e.g. 8 bytes for a
double
)
- Reference Types:
- Enough memory is set aside to hold an address
(i.e., 4 bytes)
Expressions Revisited
- Recall:
- An expression is a syntactically valid construct that
can be evaluated (i.e., results in a value)
- The Type of an Expression:
- An expression is said to be of the type
of the value it evaluates to