1. Primitive Types and Precision
Primitive Types
Java has 8 primitive types that are built into the language specification: byte, short, int, long, float, double, boolean, and char. All other types (such as String) are supplied via libraries, some of which are included with Java when you download the JRE or JDK. The primitives are:
byte: The
byte
type is an 8-bit signed two's complement integer. The range of a byte goes from -128 to 127. It may be useful to use bytes to save memory compared to an int.short: The
short
type is a 16-bit signed two's complement integer. The range of a short goes from -32,768 to 32,767 .int: The
int
type is a 32-bit signed two's complement integer. The default range is two to the power of 31 (minus 1) to negative two to the power of 31. Advanced programmers should note that in Java 8, int can also represent unsigned 32-bit integers. These have a range from 0 to 2 to the power of 32 (minus 1). See the documentation on Integer and Number for more information about how to perform operations on unsigned integers.long: The
long
type is a 64-bit two's complement integer. The signed long has a minimum value of -2 to the power of 63 and a maximum value of 2 to the power of 63 (minus 1). Like int, Java 8 now supports unsigned longs.float: The
float
type is a single-precision 32-bit IEEE 754 floating point.double: The
double
type is a double-precision 64-bit IEEE 754 floating point.boolean: The
boolean
type has only two possible values:true
andfalse
. Note that unlike C++, true is not equivalent to 1, and false is not equivalent to 0.char: The
char
type is a single 16-bit Unicode character. We will discuss the char type in more detail when we learn about Strings.
Floating Point Precision
Floating point numbers do not perfectly represent decimal values from math. You can imagine these numbers as being written on a short piece of paper, and so trailing values far right of the decimal point are often lost. The numbers are presented in binary with a variable number of whole number digits. Thus, numbers that seem round (like 1.3) may actually stored only approximately. Java will often round numbers when printing, so you cannot assume that two floating point numbers are equivalent.
These examples uses material from later sections:
public class MyProgram { public static void main(String[] args) { double a = 0.7; double b = 0.9; double x = a + 0.1; //0.8 double y = b - 0.1; //0.8 //The == operator can be used to compare two numbers: System.out.println(x == y); //Prints false //x and y are not equal, even though they would be in math. } }
Advanced programmers should instead subtract two numbers and check whether the difference is small enough.
public class MyProgram { public static void main(String[] args) { double a = 0.7; double b = 0.9; double x = a + 0.1; //0.8 double y = b - 0.1; //0.8 System.out.println(x == y); //Prints false //Math.abs is absolute value: System.out.println(Math.abs(x - y) < 0.0001); //Prints true } }
Use double rather than float when you need more precision. For arbitrarily high precision, research the BigDecimal class in Java.