Unit 4: Fundamental Data Types Flashcards
Primitive Data Types
Variables of Primitive types store values.
Do not have methods.
Integers are whole numbers (+, -, 0).
Floating numbers are real numbers.
Why can’t we just use floating point numbers?
Integers require less memory space.
Floating numbers have problems with rounding.
Integers are processed faster.
Values of int, & double
int = 4 bytes double = 8 bytes
int is default integer type in Java
stores integer value in range -2,147,483,648 . . . 2,147,483,647.
double is default floating-point type in Java
stores double-precision.
floating point value in approx. range +- 10^308, with 15 significant decimal digits.
boolean
stores boolean value: true or false
Multiple variables of same type can be declared in one statement
EX:
double gpa, price, milesPerGallon;
Bug lady, cricket, ant;
Convenient to initialize several variables to same value in one statement
EX:
gpa = price = milesPerGallon = 0.0;
Stores to the left.
Read from RIGHT to LEFT.
Aliases
2 or more object variables refer to same object.
Operators
5 operators (arithmetic). Combines 2 or more values or expressions. \+ Addition - Subtraction * Multiplication / Division % Modulo or Remainder
Integer Division
Integer division by integer is ALWAYS integer.
Remainder/Modulo
Amount leftover after integer division.
What happens if we try to change the value of our Constants?
COMPILE ERROR!!!
Can a double be assigned to an int?
No! The decimal portion and 4 of the 8 bytes would have to be discarded.
Java won’t “demote” on it’s own.
Can an int be assigned to a double?
Yes! To store a 4-byte integer in an 8-byte floating point number, Java just adds a decimal point.