C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TYPE: This returns a class reference. This matches the shortened type, like int for Integer.
SIZE: This returns the numbers of bits in the type. There are 8 bits in a byte.
MIN_VALUE: This is the smallest value the numeric class can represent. All the types except Char have sign bits.
MAX_VALUE: This is the highest value possible in a type. For Double and Long, these are huge.
Java program that shows type information
public class Program {
public static void main(String[] args) {
// Display common numeric types.
System.out.println(Byte.TYPE);
System.out.println(Byte.SIZE);
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Short.TYPE);
System.out.println(Short.SIZE);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println(Character.TYPE);
System.out.println(Character.SIZE);
System.out.println((int) Character.MIN_VALUE);
System.out.println((int) Character.MAX_VALUE);
System.out.println(Integer.TYPE);
System.out.println(Integer.SIZE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Float.TYPE);
System.out.println(Float.SIZE);
System.out.println(Float.MIN_VALUE);
System.out.println(Float.MAX_VALUE);
System.out.println(Double.TYPE);
System.out.println(Double.SIZE);
System.out.println(Double.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Long.TYPE);
System.out.println(Long.SIZE);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
}
}
Output
byte
8
-128
127
short
16
-32768
32767
char
16
0
65535
int
32
-2147483648
2147483647
float
32
1.4E-45
3.4028235E38
double
64
4.9E-324
1.7976931348623157E308
long
64
-9223372036854775808
9223372036854775807
And: To loop through the entire range of positive ints, we can use a for-loop and stop at MAX_VALUE.
Caution: We must use "less than" Integer.MAX_VALUE not <= because otherwise the loop index will overflow.
Java program that uses Integer.MAX_VALUE, loops
public class Program {
public static void main(String[] args) {
// Loop over all positive integers.
for (int value = 0; value < Integer.MAX_VALUE; value++) {
// ... Do something important.
}
// Loop over all positive integers in reverse.
for (int value = Integer.MAX_VALUE - 1; value >= 0; value--) {
// ... Do another important thing.
}
System.out.println("DONE");
}
}
Output
DONE
Java program that uses byte
public class Program {
public static void main(String[] args) {
byte value = 100;
System.out.println(value);
// Try to cast a large number to a byte.
// ... The result may be unexpected.
value = (byte) 9999;
System.out.println(value);
}
}
Output
100
15
Java program that uses short
public class Program {
public static void main(String[] args) {
// Use a short local variable type.
short number = 10;
System.out.println(number);
// Short has less range than int.
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
}
}
Output
10
-32768
32767
Java program that uses long
public class Program {
public static void main(String[] args) {
// This number is too long to be an int, but it fits in a long.
String value = "100000000000000000";
long result = Long.parseLong(value);
System.out.println(result);
}
}
Output
100000000000000000
Here: We see how the bits change in the numbers 0 through 9. ToBinaryString helps us visualize bitwise operations.
Java program that uses toBinaryString
public class Program {
public static void main(String[] args) {
// Convert 1 to 10 to binary Strings.
for (int i = 0; i < 10; i++) {
String result = Integer.toBinaryString(i);
System.out.println(result);
}
}
}
Output
0
1
10
11
100
101
110
111
1000
1001