Unit 01 Wed - Primitive Types
Homework
public class Variables {
public static void main(String[] args) {
int x;
x = 90;
x = 5;
x = x + 1;
System.out.println("The variable x = " + x);
}
}
Variables.main(null)
Primitive Data Types
- Byte: minimum value of -128 and maximum value of 127 (inclusive). It is useful for saving memory in large arrays
- Short: Minimum value of -32,768 and maximum value of 32,767. Same purpose as Byte
- Int: Any integer or whole number
- Long: Greater range than int
- Float: floating point numbers that tend to have decimals
- Double: not good for precise data
- Boolean: logic that evaluates whether a condition is true or false
- Char: data type that is used to store a single character (must be in 'single quotes')
public class PrimitiveDataTypes {
public static void main(String[] args) {
int a = 5;
double b = 5.0;
b = 3; // only works for double
a = (int) 5.999; // casting cuts off everything after the decimal
System.out.println(a);
boolean c = true;
c = false;
System.out.println(c);
char d = 'D';
System.out.println(d);
}
}
PrimitiveDataTypes.main(null)
public double purchasePrice () {
double taxTotal = getListPrice() * taxRate;
return taxTotal + getListPrice();
}
public int compareCustomer (Customer other) {
int c = getName().compareTo(other.getName());
if (c != 0) {
return c;
}
else {
int rd = other.getID();
if (getID() = rd) {
return 1;
}
else if (getID() < rd) {
return -1;
}
else {
return 0;
}
}
}