Java Programming/Primitive Types

Primitive types are the most basic data types available within the Java language. There are 8:, , , , , , and. These types serve as the building blocks of data manipulation in Java. Such types serve only one purpose &mdash; containing pure, simple values of a kind. Because these data types are defined into the Java type system by default, they come with a number of operations predefined. You can not define a new operation for such primitive types. In the Java type system, there are three further categories of primitives:
 * Numeric primitives:, , , and . These primitive data types hold only numeric data. Operations associated with such data types are those of simple arithmetic (addition, subtraction, etc.) or of comparisons (is greater than, is equal to, etc.)
 * Textual primitives: and . These primitive data types hold characters (that can be Unicode alphabets or even numbers). Operations associated with such types are those of textual manipulation (comparing two words, joining characters to make words, etc.). However,  and  can also support arithmetic operations.
 * Boolean and null primitives: and.

All the primitive types have a fixed size. Thus, the primitive types are limited to a range of values. A smaller primitive type can contain less values than a bigger one.

Integer primitive types silently overflow:

As Java is strongly typed, you can't assign a floating point number (a number with a decimal point) to an integer variable:

A primitive type should be set by an appropriate value. The primitive types can be initialized with a literal. Most of the literals are primitive type values, except String Literals, which are instance of the class.

Numbers in computer science
Programming may not be as trivial or boring as just crunching huge numbers any more. However, huge chunks of code written in any programming language today, let alone Java, obsessively deal with numbers, be it churning out huge prime numbers, or just calculating a cost of emission from your scooter. In 1965, Gemini V space mission escaped a near-fatal accident caused by a programming error. Again in 1979, a computer program overestimated the ability of five nuclear reactors to withstand earthquakes; the plants shut down temporarily. There is one thing common to both these programming errors: the subject data, being computed at the time the errors occurred, was numeric. Out of past experience, Java came bundled with revised type checking for numeric data and put significant emphasis on correctly identifying different types of it. You must recognise the importance of numeric data when it comes to programming.

Numbers are stored in memory using a binary system. The memory is like a grid of cells:

Each cell can contain a binary digit (shortened to bit), that is to say, zero or one:

Actually, each cell does contain a binary digit, as one bit is roughly equivalent to  and an empty cell in the memory signifies. A single binary digit can only hold two possible values: a zero or a one.

Multiple bits held together can hold multiple permutations &mdash; 2 bits can hold 4 possible values, 3 can hold 8, and so on. For instance, the maximum number 8 bits can hold ( in binary) is   in the decimal system. So, the numbers from 0 to 255 can fit within 8 bits.

It is all good, but this way, we can only host positive numbers (or unsigned integers). They are called unsigned integers. Unsigned integers are whole number values that are all positive and do not attribute to negative values. For this very reason, we would ask one of the 8 bits to hold information about the sign of the number (positive or negative). This leaves us with just 7 bits to actually count out a number. The maximum number that these 7 bits can hold is   in the decimal system.

Positive numbers

Negative numbers

Altogether, using this method, 8 bits can hold numbers ranging from  to   (including zero) &mdash; a total of 256 numbers. Not a bad pay-off one might presume. The opposite to an unsigned integer is a signed integer that have the capability of holding both positive and negative values.

But, what about larger numbers. You would need significantly more bits to hold larger numbers. That's where Java's numeric types come into play. Java has multiple numeric types &mdash; their size dependent on the number of bits that are at play.

In Java, numbers are dealt with using data types specially formulated to host numeric data. But before we dive into these types, we must first set some concepts in stone. Just like you did in high school (or even primary school), numbers in Java are placed in clearly distinct groups and systems. As you'd already know by now, number systems includes groups like the integer numbers (0, 1, 2 ... ∞); negative integers (0, -1, -2 ... -∞) or even real and rational numbers (value of Pi, ¾, 0.333~, etcetera). Java simply tends to place these numbers in two distinct groups, integers (-∞ ... 0 ... ∞) and floating point numbers (any number with decimal points or fractional representation). For the moment, we would only look into integer values as they are easier to understand and work with.

Integer types in Java
With what we have learned so far, we will identify the different types of signed integer values that can be created and manipulated in Java. Following is a table of the most basic numeric types: integers. As we have discussed earlier, the data types in Java for integers caters to both positive and negative values and hence are signed numeric types. The size in bits for a numeric type determines what its minimum and maximum value would be. If in doubt, one can always calculate these values.

Lets see how this new found knowledge of the basic integer types in Java fits into the picture. Say, you want to numerically manipulate the days in a year &mdash; all 365 days. What type would you use? Since the data type  only goes up to 127, would you risk giving it a value greater than its allowed maximum. Such decisions might save you from dreaded errors that might occur out of the programmed code. A much more sensible choice for such a numeric operation might be a. Now, why couldn't they make just one data type to hold all kinds of numbers? Let's explore why.

When you tell a program you need to use an integer, say even a, the Java program allocates a space in the memory. It allocates whole 8 bits of memory. Where it wouldn't seem to matter for today's memory modules that have place for almost a dozen trillion such bits, it matters in other cases. Once allocated that part of the memory gets used and can only be claimed back after the operation is finished. Consider a complicated Java program where the only data type you'd be using would be  integers. What happens when there's no space for more memory allocation jobs? Ever heard of the Stack Overflow errors. That's exactly what happens &mdash; your memory gets completely used up and fast. So, choose your data types with extreme caution.

Enough talk, let's see how you can create a numeric type. A numeric type begins with the type's name (, , etc.) and then provides with a name for the allocated space in the memory. Following is how it's done. Say, we need to create a variable to hold the number of days in a year.

Here,  is the name of the variable that holds   as its value, while   is the data type for that particular value. Other uses of integer data types in Java might see you write code such as this given below:

Integer numbers and floating point numbers
The data types that one can use for integer numbers are,  ,   and   but when it comes to floating point numbers, we use   or. Now that we know that, we can modify the code in the code section 3.53 as:

Why not, you say? If we'd used a, we would have to append the number with a   as a suffix, so   should be   as in:

Floating-point math never throws exceptions. Dividing a non-zero value by  equals. Dividing a non-infinite value by  equals.

Question 3.7: Consider the following code:

These are five variables. There are a, a  , a  , a   and a. Retrieve the type of each one.


 * can only be the  because only a boolean can handle boolean values.
 * can only be the  because only a char can contain a character.
 * can only be the  because only a double can contain a decimal number here.
 * is the  because a   can not contain such a low value.
 * is the remaining one so it is the.

Data conversion (casting)
Data conversion (casting) can happen between two primitive types. There are two kinds of casting:
 * Implicit: casting operation is not required; the magnitude of the numeric value is always preserved. However, precision may be lost when converting from integer to floating point types
 * Explicit: casting operation required; the magnitude of the numeric value may not be preserved

The following table shows the conversions between primitive types, it shows the casting operation for explicit conversions:

Unlike C, C++ and similar languages, Java can't represent  as   or   and can't represent   as non-zero. Java can't cast from boolean to a non-boolean primitive data type, or vice versa.

For non primitive types: