How to use Java primitives
There are 8 primitive data types in Java, which all have a fixed size and a defined value range. They’re used to create variables and assign them individual numbers, characters or logical values. The 8 Java primitives are boolean, byte, char, double, float, int, long and short.
What are Java primitives?
Like other programming languages, Java has various data types. In Java, these fall into two categories. The first is reference types, which refer to an object that can be freely defined, like strings, arrays, classes and interfaces. Then there are the Java primitives. They have a fixed size that is the same across platforms. They also have their own wrapper class, are immutable and have a fixed range of values. Java primitives are used to create variables for individual numbers, characters or logical values.
In Java, there are 8 different primitive data types that differ primarily in terms of which values they store. What they all have in common is that they can only hold one single value. Since some of them are used in similar situations, we can further divide the Java primitives into four categories:
- Logical data type (boolean)
- Integral data type (byte, short, int and long)
- Floating point data type (float and double)
- Character data type (char)
Since they are stored directly in the stack, their sizes are of special importance. We’ll discuss this more further on in this guide. Java primitives are also important because Java is a statically typed language. So when you’re creating a program, you already need to define which data type a variable is, otherwise you’ll run into error messages. Here’s an overview of the Java primitives:
Data type | Size | Value range | Default value | Wrapper class |
---|---|---|---|---|
boolean | 1 bit | true or false | false | java.lang.Boolean |
byte | 8 bits | -128 to 127 | 0 | java.lang.Byte |
short | 16 bits | -32768 to 32767 | 0 | java.lang.Short |
int | 32 bits | -2147483648 to 2147483647 | 0 | java.lang.Integer |
long | 64 bits | -9223372036854775808 to 9223372036854775807 | 0 | java.lang.Long |
float | 32 bits | up to 7 decimal places | 0,0 | java.lang.Float |
double | 64 bits | up to 16 decimal places | 0,0 | java.lang.Double |
char | 16 bits | **’\u0000’ (ie 0) to ‘\uffff’ (corresponds to 65535) | ‘\u0000’ | java.lang.Character |
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How to use the Java primitive boolean
A Java Boolean is also known as a truth value. It is the most simple Java primitive, as it has just two possible values: “true” or “false”. It’s used when a logical operand is needed and is the only logical data type. In expressions, its two possible values usually stand for a condition that’s either fulfilled (true) or not (false). If you don’t assign a value to a Boolean, it will have the default value false. The Boolean can be combined with Boolean operators in Java like AND and OR. Assigning a value to a Boolean looks as follows:
public class Main {
public static void main(String[] args) {
boolean x = true;
System.out.println(x);
}
}
javaIf you use the Java command System.out.prinln
to initialize an output, the output will look like this:
true
javabyte
byte is the smallest data type among the integer data types. It has a very restricted range of values, namely from -128 to 127. But it only uses 8 bits of memory. In fact, it gets its name from the fact that 8 bits make one byte. If you’re only working with a very limited range of values, you can declare a byte like this:
public class Main {
public static void main(String[] args) {
byte x = 101;
System.out.println(x);
}
}
javaThe output looks as follows:
101
javashort
short is twice as large as byte, meaning it’s also one of the more seldomly used Java primitives. But if byte is too small and int is too big, short might be the integer data type you’re looking for. Here’s how you declare it:
public class Main {
public static void main(String[] args) {
short x = -27412;
System.out.println(x);
}
}
javaThe output looks like this:
-27412
javaint
int is the most frequently used of the integer data types. It has a very large value range and still saves space. int is a two’s complement value and is also frequently used for other purposes. Here’s how to use it:
public class Main {
public static void main(String[] args) {
int x = 14;
int y = 3;
int z = x + y;
System.out.println(x + " + " + y + " = " + z);
}
}
javaThe output looks as follows:
14 + 3 = 17
javalong
long is the extension of int and can contain even longer numbers. int will suffice for most cases, but if you want to declare long you can do so as follows:
public class Main {
public static void main(String[] args) {
long x = 47;
System.out.println(x);
}
}
javaHere’s the output:
47
javafloat
There are two Java primitives for representing subsets of rational numbers. float is the smaller of the two floating point data types and uses 32 bits. It can display up to 7 decimal places. However, it’s not very precise and thus not used very often. If you do decide to use it, you can declare it as follows:
public class Main {
public static void main(String[] args) {
float x = 7.22f;
System.out.println(x);
}
}
javaNote that you should insert a lowercase or uppercase “f” after the number, to tell the computer that it is a float and not a double. The f isn’t shown in the output:
7.22
javadouble
The second floating point data type is double. It’s significantly more precise than float but still doesn’t return completely exact results. If you’re looking for an alternative, you can turn to the class BigDecimal. If double suits your purposes, here’s how you declare it:
public class Main {
public static void main(String[] args) {
double x = 7.2252;
System.out.println(x);
}
}
javaNote there is no extra letter here. The output looks as follows:
7.2252
javachar
char is used to represent Unicode characters. It goes from ‘\u0000’ to ‘\uffff’, so from 0 to 65535. The character data type can represent almost all European and Asian characters. It uses 16 bits of memory. The values of this Java primitive are set in single quotation marks. Here’s how it looks in code:
public class Main {
public static void main(String[] args) {
char x = '&';
System.out.println(x);
}
}
javaThe output looks as follows:
&
java