Introduction to primitive data types.
Objectives
We would like to :
- List the basic or primitive data types in Java
- Describe what each type holds.
Notes
- In the book, this is section 2.2.2 Types
- In java there are 8 primitive types of data.
- They are called primitive because they are the most basic types available.
- Other types are constructed from these types.
- They are the building blocks.
- Four are integer types.
- An integer is a counting number, zero or the negative of the counting numbers.
- Just like math, ... -3, -2, -1, 0, 1, 2, 3, ...
- But we don't have the ... in programming.
- Our memory is limited, so we have maximum and minimum values we can store.
- We give different amounts of storage for different integer types.
- Think about score boards.
- Football scoreboards have two digits
- Thus scores between 0 and 99 can be represented.
- Basketball scoreboards have three digits
- In this case scores between 0 and 199 (or 999) can be represented.
- The number of digits determine the range.
- For memory we store data in bits (0 or 1) or bytes (8 bits)
- Integer types
-
byte
holds 8 bits, or -128 to 127
-
short
holds 16 bits or -32,768 to 32,767
-
int
holds 32 bits or -2,147,483,648 to 2,147,483,647.
-
long
holds 64 bits or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- In general, unless you have a reason, use the type
int
- Floating point numbers, or floats, are numbers
- with a decimal point 3.14159
- or in scientific notation 6.023x1023
- There are two floating point types
- Floating point numbers are a bit more complex
- The suffer from round off error.
- We will discuss this later.
- Other types
- A
char
- Is a character.
- This is anything in Unicode.
- For now think
- letter: a, b, c
- single digits: 0, 1, 2, 3
- Symbols: $, &, ,
- Anything you can type
- But there are more things.
- Normally we put characters inside of ' marks ('a')
- Some are escaped
- '\n' is the newline character
- '\t' is the tab character
- eventually a collection of characters is called a string
- These are inside " ("Hello World!")
- We have seen these
- We will talk about these again in the future.
- A
bool
is a boolean value
- Only two values, true or false.
- We will use these extensively when we start decisions and looping.