No products in the cart!
Please make your choice.View all catalog
You may have heard it said before that Java is a strongly typed programming language. That means that every variable that is defined has to be assigned a data type, which determines what kind of data it can hold for the duration of program execution. In other words, once defined, a variable cannot change type…at least not without recompilation. With that in mind, you may want to think carefully before assigning a variable’s type, as choosing the wrong kind can have fairly serious repercussions.
This programming tutorial will list all of the primitive data types supported by the Java language, as well as provide a description of each, so that developers will be better equipped to match variables to their appropriate types.
Looking to learn Java software development in a class or online course? We have a list of the Best Online Courses to Learn Java to help get you started.
Broadly speaking, Java data types can be classified into two types: primitive and non-primitive.
Primitive data types include:
In addition to the values it may contain, the data type also determines the operations that may be performed on it. Moreover, primitive values do not share state with other primitive values, and are, as such, completely discreet and autonomous. You can think of them as the building blocks for the non-primitive data types.
Meanwhile, non-primitive types are those which refer to things, and are thus termed reference types. As objects, non-primitive types can include methods that perform actions. Strings, Arrays, Classes, and Interfaces are all examples of non-primitive types.
There are a few other differences between primitive and non-primitive types, but we will cover those a little later on, as we go over the primitive data types available in Java in more detail.
Read: What are Variables in Java?
As we will see in this section, each primitive data type allocates a certain amount of memory and comes pre-initialized with its own default value.
The byte data type is a very small 8-bit signed integer. The term “signed” means that it can represent both positive and negative numbers. It has a minimum value of -128 and a maximum value of 127, inclusively. Its small size makes it ideal for using in large arrays, where the memory savings can be substantial. They can also be used in place of int, where their strict size limits may help to notify other developers that a variable is meant to store fairly small numbers, like a person’s age. Its default value is 0.
Here are some examples of how to use byte data types in Java:
byte b = 99; byte b2;
The above code would add the value 99 to the byte data type named b. The second line initialized a byte named b2, which would contain the default value of 0, since we did not assign it a value.
With its 16-bit size, the short data type fits neatly between a byte and int. It has a much larger range of -32,768 to 32,767 inclusive. It is also great for managing memory and informing other developers about the kinds of numbers it may contain. Its default value is 0.
Here are some examples of how to use the short data type in Java:
short s = 29999; short s2;
The int data type is an interesting data type in that it can be either signed or unsigned. As a 32-bit signed integer (the default), it has a minimum value of -231 and a maximum value of 231-1, or -2,147,483,648 to 2,147,483,647. Declaring an int as an unsigned integer shifts its range to the positive side of the scale, giving it a minimum value of 0 and a maximum value of 232-1, or 4,294,967,295. Its default value is 0.
Here are some examples of how to use the int data type in Java:
int a = 456789; int b;
The long data type has even more storage capacity than an int, requiring 64 bits. Like the int, the long type can also be signed or unsigned. The signed long has a minimum value of -263 and a maximum value of 263-1, or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Meanwhile, an unsigned 64-bit long has a minimum value of 0 and a maximum value of 264-1, or a whopping 18,446,744,073,709,551,615! Its default value is 0L.
Here are some examples of how to assign values to a long data type in Java:
long l = 1556667890L; long l_2;
The float data type is a 32-bit single precision floating point. While large enough to store factional numbers between 3.4e-038 and 3.4e+038 with up to seven decimal digits, once the value gets past six decimal points, the number will become less precise and more of an estimate. Hence, if you require precise values, such as for currency, you will need to use the java.math.BigDecimal class instead. Its default value is 0.0f.
Here are some examples showing how to assign values to a float data type in Java:
float myFloat = 256.8f; float myFloat2;
The double data type is a double-precision 64-bit floating point, large enough for storing fractional numbers between 1.7e-308 and 1.7e+308 up to 15 decimal digits. It suffers from the same limitation limit as float does and should therefore only be used if approximation errors are acceptable. Its default value is 0.0d.
Here are some examples of how to assign values to a double data type in Java:
double myDouble = 256.7879837; double myOtherDouble;
Read: Java Tools to Increase Productivity
From the largest to smallest data type, the boolean has only two possible values: true and false. It stores its value in a single bit. However, for convenience, Java pads the value and stores it in a single byte. One limitation of the boolean data type is that, while fine for simple flags that track true/false conditions, it does not have an “unset” value, which can lead one to erroneously conclude that a value is false, when it is really not set. Its default value is false.
Here are some examples of how to assign values to a boolean data type in Java:
boolean myBool = true; boolean myBool2;
The char data type stores one single 16-bit Unicode character. It has a minimum value of ‘\u0000‘ (or 0) and a maximum value of ‘\uffff‘ (or 65,535 inclusive). Its default value is ‘\u0000‘.
Here are some examples of how to use the char data type in Java:
char myChar1 = 'A'; char myChar2 = 65;
In Java, primitive data types are distinct containers for values that do not share state with other primitive values. They also act as the building blocks for the non-primitive data types. One thing that was not mentioned previously is that primitive variables are passed to methods by value, which means that methods receive a copy of the value. That allows developers to make changes to the variable within a method without affecting the original.
Read more Java programming tutorials and software development tips.
What is Lean Development? | Developer.com
Instagram will shove ads into more parts of the app