Java Data Types
Java has two groups of data types: primitive types (simple values) and reference types (objects like String, arrays and classes).
The 8 primitive types
| Type | Size | What it keeps | Example |
|---|---|---|---|
| byte | 8 bit | Very small whole number (-128 to 127) | byte b = 100; |
| short | 16 bit | Small whole number | short s = 3000; |
| int | 32 bit | Whole number. The default for numbers | int n = 42; |
| long | 64 bit | Big whole number | long big = 5000000000L; |
| float | 32 bit | Decimal number (about 7 digits) | float f = 3.14f; |
| double | 64 bit | Decimal number (about 15 digits). The default for decimals | double d = 3.14159; |
| char | 16 bit | One character | char c = 'A'; |
| boolean | 1 bit of information | true or false | boolean ok = true; |
The real limits
You do not need to remember the numbers. Java gives them to you:
public class Main {
public static void main(String[] args) {
System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
System.out.println("float max: " + Float.MAX_VALUE);
System.out.println("double max: " + Double.MAX_VALUE);
}
}
byte: -128 to 127
short: -32768 to 32767
int: -2147483648 to 2147483647
long: -9223372036854775808 to 9223372036854775807
float max: 3.4028235E38
double max: 1.7976931348623157E308
Examples of each type
public class Types {
public static void main(String[] args) {
byte b = 100;
short s = 3000;
int n = 42;
long big = 5_000_000_000L; // L at the end, and _ makes it easy to read
float f = 3.14f; // f at the end
double d = 3.14159;
char c = 'A';
boolean ok = true;
System.out.println(b + " " + s + " " + n + " " + big);
System.out.println(f + " " + d + " " + c + " " + ok);
}
}
100 3000 42 5000000000
3.14 3.14159 A true
Overflow
When a value is too big for its type, it does not give an error. It wraps around to the other side:
public class Overflow {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println(max);
System.out.println(max + 1); // wraps to the smallest int
}
}
2147483647
-2147483648
The char type
A char keeps one character in single quotes, and it is really a number (the Unicode code):
public class Chars {
public static void main(String[] args) {
char c = 'A';
System.out.println(c);
System.out.println((int) c); // 65
System.out.println((char) 66); // B
System.out.println(c + 1); // number: 66
}
}
A
65
B
66
Decimal numbers are not exact
public class Decimals {
public static void main(String[] args) {
System.out.println(0.1 + 0.2);
System.out.println(0.1f + 0.2f);
}
}
0.30000000000000004
0.3
Computers keep decimals in binary, so some values are close but not exact. For money use BigDecimal or keep the amount in paise or cents as a long.
Reference types
Everything else is a reference type: String, arrays, and all classes. A reference variable keeps the address of an object, and it can be null (no object). Each primitive type has a wrapper class too (Integer, Double, Boolean and so on), which we use with collections.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |