- 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
Variable/Constant 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
  • 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
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

Non-Atomic Types in Java
Back SMYC Forward
  • 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
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
Names/Identifiers in Java
Back SMYC Forward
  • 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.)
Back SMYC Forward
Nerd Humor
http://imgs.xkcd.com/comics/x.png
(Courtesy of xkcd)
Java vs. Python - Important Differences
Back SMYC Forward
  • Name/Identifier Restrictions:
    • In Python, identifiers can start with an '_'
  • Variable Types:
    • In Python, all variables actually contain references
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;
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)
Java vs. Python - Important Differences
Back SMYC Forward
  • 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
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 -