Data
Tonga Institute of Higher Education
Variables Programs need to remember values.
Example: A program that keeps track of sales needs to remember data: A loaf of bread was sold to Sione Latu on 14/02/04
for T$1.00. Customer Name: Sione Latu Date Sold: 02/14/04 Item Sold: Loaf of Bread Item Cost: T$1.00
Variable – Place in the computer’s memory where information is stored.
Value Types vs. Reference Types
A variable stores a value or object.
Value Types only store values int only stores:
1 2 5
boolean only stores: True False
Reference Types only store references to an object Object - A self-contained entity that contains data and procedures to
manipulate the data. A reference is a location in memory The location is the starting point of an object
Primitive Data Types
Type Size (bits) Value Range
byte 8 -128 to 127
short 16 -32,768 to 32767
int 32 -2,147,483,648 to 2,147,483,647
long 64 -9.2 x 1015 to 9.2 x 1015
float 32 -3.4 x 1038 to 3.4 x 1038
double 64 -1.8 x 10308 to 1.8 x 10308
char 16 0 to 65,535
boolean 1 True or False
Primitives - The basic types supported by the programming language.Use lowercase for primitive data types in code
Primitive Data Type Selection Practice
What is a good data type for: Someone’s age? A customer’s identification number for a video rental store in
Nuku’alofa? A very large number with decimals? The price of an item?
How to choose the right primitive variable type. Byte, short, int and long store whole numbers. Float and double store fractions. Double is more accurate, so if
accuracy is important, use double. If you pick something that is too big, you’re wasting memory space.
Wasting memory space slows things down. If you pick something that is too small, then you’re going to crash. If you need a character, use char. If you need a true/false value, use boolean.
Using Primitive Variables
2 Steps to using variables1. Declare the variable
2. Initialize the variable
Declaring Primitive Variables Declare the variable – Tell the computer to reserve a space in memory
for the variable.
You need to tell the computer 2 things:1. Name of the variable2. Type of the variable (What kind of variable you have)
Primitive types byte short int long float double char boolean
Type
Name
Declaring Primitive Variables Guidelines Use a name that is easy to remember.
Do not use x, y, z Variable names must start with a letter, underscore or
dollar sign. Begin variables with a lowercase character. Then, use a
capital letter for each word. Examples
firstName customerID
Initializing Primitive Variables Initialize the variable – Assign an initial value
to a variable. Char values must be enclosed in single quotes. String values must be enclosed in double quotes. Boolean value should be True or False
Initial Value
Declaring and Initializing Primitive Variables in 1 line You can declare and initialize a variable in 1 line.
Demonstration
Declaring and Initializing Variables
Converting Data TypesSmaller to Larger
It is possible to automatically move a value from a smaller data type to larger data type
Example MaximumShort Value
Maximum Long ValueIs 9.2 x 1015
Converting Data TypesLarger to Smaller
If a value from a larger data type is moved into a smaller data type, the value may be too big for the new data type
This will cause an error
Using this:
Results in:
Do not do this!
MaximumShort Valueis 32767
Converting Data Types Force conversion of data types by casting it. To cast, use this format:
(<Desired data type>)<Variable name>
If the Cast function isn’t able to convert the value, you may not get the answer you expect.
int is biggerthan a byte
Without this, we would get an error
Forcing a cast from a larger datatype to a smaller data type is not recommended!
But the final valueIs -24, which isstrange!
Demonstration
Converting Data Types
Arithmetic Operators
Declare and Initialize x, y and z
Get values from x and y Adds x and y together Assigns the sum of x an y to z
Operator Meaning Example
+ Addition 11 + 22
- Subtraction 22 – 11
* Multiplication 5 * 6
/ Division 21 / 3
% Modulus 12 percent 2
String Concatenation Addition
You can add strings. Adding strings is called concatenation.
This converts all non strings into strings. Make sure you add your numbers before converting them to strings.
Demonstration
String Concatenation
Division Tricky because result
may not be an integer. In this case the number gets cut off. NOT rounded!
Even having a result of float doesn’t work. Because Java has rules for dealing with data types. The result of a division between two integers is always an integer. So the float just added a .0 to it.
To get around this, we need to divide two floats!
Demonstration
Division
Multiplication and Modulus Multiplication
Use asterisk (*) instead of x
ModulusThe remainder of a divisionCan be used to determine whether a number
is divisible by another numberCan also determine if a number is even or odd
Order of Operations
When you have a lot of operations, they are performed in a certain order.
Parentheses () Multiplication or Division operations from left to right Addition or Subtraction operations from left to right.
Examples: 3 + 6 + 9 / 3 = 3 + 6 + 3 = 12 (3 + 6 + 9) / 3 = 18 / 3 = 6