- Forward


Data Types and Variables
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Data and Values
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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
Back SMYC Forward
images/counting-numbers.gif
Data Representation - Negative Integers
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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.)
Back SMYC Forward
images/twos_complement.gif
Going Further: Negative Numbers (cont.)
Back SMYC Forward
http://imgs.xkcd.com/comics/cant_sleep.png
(Courtesy of xkcd)
Data Representation - Real Numbers
Back SMYC Forward
  • 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.)
Back SMYC Forward
  • Terms:
    • Sign
    • Exponent
    • Mantissa
  • Normalization:
    • One digit left of the decimal
  • Example: +1.101101 x 23
    • Sign: +
    • Exponent: 3
    • Mantissa: 1.101101
Going Further: Real Numbers (cont.)
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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.)
Back SMYC Forward
  • The ASCII (American Standard Code for Information Interchange) Encoding:
    • images/ascii.gif
  • Unicode:
    • A mapping for every character in every language (including many dead languages)
Some Atomic/Primitive/Fundamental Types in Java
Back SMYC Forward

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
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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
Some Non-Atomic Types in Java
Back SMYC Forward
  • String java.lang.String :
    • Essentially a collection of char values
  • Color java.awt.Color :
    • Essentially a collection of int values
  • BufferedImage java.awt.image.BufferedImage :
    • Essentially a collection of Color references
Variables in Java
Back SMYC Forward
  • 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.)
Back SMYC Forward
Nerd Humor
http://imgs.xkcd.com/comics/x.png
(Courtesy of xkcd)
Variable Declarations
Back SMYC Forward
  • 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
Back SMYC Forward
  • Syntax: Click here for information.
    • 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.)
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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
There's Always More to Learn
Back -