Java primitives are predefined basic data types that exist in the Java programming language. We do not need to create them as we would with our custom Java classes.
What Are Basic Data Types in Java?
Java primitive types hold primitive values. Java has these types – byte, short, int, long, float, double, boolean, and char.
The Integer Types – byte, short, int, and long
The byte, short, int, and long types are integer types, and they hold whole-number numeric values. These data types differ from each other in the range of values they can have. Consider the following table.
Primitive Type | Description | Range |
---|---|---|
byte | The byte data type is an 8-bit signed two's complement integer | -128 to 127 (inclusive) |
short | The short data type is a 16-bit signed two's complement integer. | -32,768 to of 32,767 (inclusive) |
int | The int data type is a 32-bit signed two's complement integer | -2^31 to 2^31-1 |
long | The long data type is a 64-bit two's complement integer. | -2^63 to 2^63-1 |
Integer Literals
Integer literals are hard-coded values in our codes. Hence, literals. Consider the following examples.
1 2 3 4 5 6 7 8 9 10 11 | // Creates and initializes a byte variable byte myByte = 1; // Creates and initializes a short variable short myShort = 2; // Creates and initializes a int variable int myInt = 3; // Creates and initializes a long variable long myLong = 4; |
The Floating-Point Types – float and double
The Java primitive float and double types are floating-point types, and they can hold non-integer fractional numbers, e.g., 12.88.
Primitive Type | Description | Range |
---|---|---|
float | The float data type is a single-precision 32-bit IEEE 754 floating point | https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 |
double | The double data type is a double-precision 64-bit IEEE 754 floating point. | https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 |
Floating-Point Literals
Like integer literals, floating-point literals are hard-coded values that go with our codes. Consider the following examples.
1 2 3 4 5 | double myDoubleA = 4.65; double myDoubleB = 4.65; float myFloatA = 1.23F; float myFloatB = 1.23f; |
One thing to note here is double is the default type of any floating-type literals. For example, 4.65. If we want to make floating-type literals of float type, we need to use the suffix F or f, e.g., 1.23F.
If we had changed 1.23F to 1.23, we would get the following compile-time error.
1 | java: incompatible types: possible lossy conversion from double to float |
The true/ false Type – boolean
A variable of a Java primitive boolean type can only hold the value true or false. Consider the following codes.
1 2 3 4 5 | boolean areYouTrue = false; boolean areYouFalse = true; System.out.println("Are you true: " + areYouTrue); System.out.println("Are you false: " + areYouFalse); |
The output is as follows.
1 2 | Are you true: false Are you false: true |
The Character Type – char
The Java primitive char type represents a single character (or, more accurately, 16-bit Unicode character) within a single-quote pair.
1 2 | char myInitial = 'K'; System.out.println(myInitial); |
The codes display K.
The range of the character type is from '\0000' (or 0) to '\uffff' (or 65,535 inclusive). Does it mean we can use char like a numeric type? Kind of, but we need typecasting. Look at these example codes. Note that we need to use typecasting on line 3 because int is larger than the char type in the entire range.
1 2 3 4 5 6 | public static void main(String[] args) { for (int i = 0; i <= 65535; i++) { char oneChar = (char)i; System.out.print(oneChar); } } |
These codes will generate the following output on the console.
Wrapper Types – Java Primitives’ Alter-egos
In general, our OOP codes cannot work directly with Java primitive types because they are not objects. We somehow need to convert them to their OOP-equivalent types, which we call Wrapper Types. Wrapper types are Java built-in classes that represent all the Java primitive types. Each primitive type has its Wrapper type.
Primitive Type | Wrapper Type |
---|---|
byte | java.lang.Byte |
short | java.lang.Short |
int | java.lang.Integer |
long | java.lang.Long |
float | java.lang.Float |
double | java.lang.Double |
char | java.lang.Character |
boolean | java.lang.Boolean |
This post is part of a reboot Java tutorial.