Chris is an instructional designer and college faculty member. He has a Master's Degree in Education and also umpires baseball.
As a programming language, Java uses two basic data types: primitive and reference/object. In this lesson, we'll have a brief look at each of Java's primitive data types as well as their use.
Primitive and Basic Data Types
When you hear the word primitive, you hopefully think of something long ago, basic, or simple. For example, a primitive shelter would be one that is made from locally-available materials and gets the job done to provide protection from the elements, but it's no five-star hotel. In Java, the most primitive elements of data are primitive data types. They serve the single purpose of comprising wholesome, simple values; and there are eight essential versions. Let's explore each of these in a categorical format, in no particular order.
Before we begin, it's important to note that the use of primitive data can be broken down into two main categories. Past that, you will see sub-categories, groups, and then finally types of data.
Boolean Data Type
Like flipping a coin, turning on a switch, or asking a basic yes-or-no question, the boolean type of data has two possible values: true or false. In terms of standards, false is the primary value and its size it 1 bit. Boolean is the tiniest Java data type given this size. It is also in a category all its own of primitive data and has neither subcategories nor groups.
Numeric Data Types
Numeric, a sub-type of primitive data, contains the remaining versions, broken down into even more categories.
Our second type of data type is known as a char. Such an item reflects characters in Unicode. As a matter of reference, Unicode refers to standard depictions of data that is text-related. This would include letters (C, K, L, etc.), special characters (cents sign, foreign currency icons), and symbols (%, #, *). More than 100,000 characters in more than 100 languages can be represented by Unicode. Char values are at 16 bits in size and set at \u0000. The char data type is the only character version but is part of the numeric sub-category of primitive data. It is also one of the more often used types.
The third type of primitive data is known as float. With a default value of 0.0f, it's size is 4 bytes. Float represents real numbers, such as 1, 2, 3, 4, etc., and also can utilize 6 significant digits. Another way to think of this is the number of spaces after a decimal point. Hopefully, that sentence didn't just bring back bad memories of grade school arithmetic! Think of money as an expression. It is listed with numerical values on both sides of a decimal point, such as 3.34 meaning three dollars and 34 cents.
Over 79,000 lessons in all major subjects
Get access risk-free for 30 days,
just create an account.
Double, the fourth version of primitive data, is related to float in the same group. It is twice the size of float at 8 bytes and contains a default value of 0.0d. Notice the similarity in the default value with the only difference being the letter d at the end. A key difference between float and double is that you should never use double for expressing money because such a value is too exact.
Byte, the fifth type of Java primitive data, is set at a size of 8 bits (1 byte) and is used for a twos complement integer. It is one of the remaining four that are integer types. Bytes represent the numeric range of -128 to 127, have a default value of 0, and represent a number in that set. They are great for keeping storage of small data.
Continuing with the integer group is the short type of primitive data. Comprising 2 bytes (or 16 bits) of integer value with a default value of 0, it has a long range. As with bytes, short data are a positive alternative to other data types where you need a larger range.
The sixth primitive data type, integer is twice the size of short and four times the size of a byte at 4 bytes (32 bits). Of all data types (both in this group, sub-category, and category), integer is the most commonly used in Java programming. It also contains a default value of 0.
The final type of data is long. The pattern of size continues here; it is twice the size of an integer at 64 bits, or 8 bytes. Still having a default value of 0, it has the longest range of any data type, and you probably will only utilize it should you have data that is over about 2 billion.
With eight versions, Java primitive data types are important to the programming language due to their purpose of providing easy, simple values to use in the code. Each applies itself to a different situation based on the parameters you require. From Boolean to char, or double to integer, their fundamental nature in Java makes them of utmost necessity.
Did you know… We have over 200 college
courses that prepare you to earn
credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the
first two years of college and save thousands off your degree. Anyone can earn
credit-by-exam regardless of age or education level.