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
Variable/Constant 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
- Declarations in Java:
- Include the type and the identifier
- 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
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 |
Non-Atomic Types in Java
- Two Observations:
- Non-atomic types are much more complicated than atomic
types
- The number of components in a non-atomic type is difficult
to determine when processing a declaration statement
- The Approach in Java:
- Allocate memory for an "address" when processing
a declaration statement that involves a non-atomic type
- Request the amount of memory needed in another statement
(and store the address of that memory in the variable)
- Java Terminology:
- Types of this kind are called reference types
- Java Conventions:
- Types of this kind have names that start with an
uppercase letter
Names/Identifiers in Java
- Language 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
Names/Identifiers in Java (cont.)
Nerd Humor
(Courtesy of xkcd)
Java vs. Python - Important Differences
- Name/Identifier Restrictions:
- In Python, identifiers can start with an '_'
- Variable Types:
- In Python, all variables actually contain references
Declaration Statements in Java
- Syntax:
-
type
identifier [, identifier]... ;
- Examples:
-
boolean done;
-
double expenses, income;
-
int numberOfChecks, styleNumber;
-
String name;
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)
Java vs. Python - Important Differences
- Declarations:
- In Python, variables are not declared
- Type Declarations:
- In Python, the type is inferred
- Type Changes:
- In Python, the type of a variable can change
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