Primitive data types are the basic building blocks of any programming language. They represent the simplest values that can be manipulated by a program, such as integers, floating-point numbers, and characters. These data types are called “primitive” because they are not composed of any other data types, unlike composite data types such as arrays or structures. The size and range of primitive data types vary depending on the programming language being used. It is essential to understand primitive data types as they are used extensively in programming, and knowledge of these types is necessary for effective data manipulation and memory management. Overall, primitive data types are a fundamental concept in programming and play a critical role in creating and manipulating data in any programming language.
Understanding Data Types
Let us first review what we have learned so far.
Computer processors process data, RAM stores the data and instructions that are directly accessed by the processor to process the data and execute the instructions. The most basic unit of information that we can store in memory(RAM is memory, memory is RAM) is bit - the capacity of a storage system that has only one of the states: 0 and 1.
Computers are sometimes classified into the following two groups:
The number of bits a computing system can process at a time.
The number of bits a computing system uses to represent addresses.
Though these two values are often the same for a computing system, they are not always the same. This leads to confusion. For example, a 32-bit machine means that its data registers are 32 bits wide, in simple words, it can store 32 bits at a time. Again by a 32-bit machine, it might also mean that to identify each address in memory the machine uses 32 bits or 32 binary digits. Larger registers make a computer faster. Addressing memory location using more bits enables a machine to support larger programs.
Though computers are classified into the above two groups where “bit” plays the main role, in a practical scenario we consider the “byte” as the most commonly used basic unit of data.
In practical usage, the most commonly used basic unit of data is byte, a group of 8 consecutive bits, as it is more meaningful.
Historically as well as typically a byte represents a character. Kilobytes, megabytes, and gigabytes are used to indicate large amounts of memory. To give you a practical example, suppose a disk has a storage capacity of 1.44 megabytes. It means that we can store approximately 1.44 million characters in that disk, or about 3,000 pages of information.
We also learned that to run any program we first store the data in the memory so that the CPU can directly access them using the memory address and process them later. A high-level programming language may use natural language elements to make the process of developing a program simpler and more understandable than using a low-level programming language as to write code in a low-level language we have to be fully aware of the details of the machine. With strong abstraction from the details of the computer, a high-level programming language is free from such constraints. We do not have to know the details of the computer architectures. A compiler or interpreter is used to transform code written in a high-level programming language into machine code. You will know about the compiler and interpreter in due time. For now, just remember that these things exist and they are some key players in the software development industry.
Now let us build our further understanding of data types based on this knowledge.
What Is A Data Type?
A data type is a term used to describe how a programming language stores data in memory, as well as the operations that can be performed on that data. A simple example of a data type is an 8-bit unsigned integer(a byte), which is stored in Memory as a single set of 8 bits and is used to store values between 0 and 255. The language will almost certainly also specify all of the standard mathematical and comparison operators that are permitted on this data type(byte). A variety of other operations on this data type may also be defined by the language.
In simple words, a data type is :
- Defined by the programming language.
- Has a defined storage mechanism within memory for the data.
- Have constraints and operations defined by the language?
Computer data is nothing more than a series of ones and zeros. The ones and zeros must be contextualized in order to convey any meaning. That context is provided by data types.
What does this string of binary digits or bits represent? It is determined by the data type. If it’s a character, it’s a lowercase ‘a’ encoded in ASCII. If it’s an integer, the answer is 97. The compiler/interpreter has no idea what to do with this context in the absence of a data type.
01100001 + 01111000
What is the outcome of this operation? Is it the letter ‘a’ + the letter ‘x’? Or 97 + 120? Is it even possible or makes sense to add these two things? These are the questions that each of these blocks of binary data with data types answers.
To give you a real-life example, When we see numbers, we usually know whether they represent a phone number, a bank balance, or a golf score. The numerals are the same; it is up to us to figure out what they mean. In binary computers, all data is simply a collection of bits. It is up to the program to make a sense of them and use them based on that interpretation to make something out of them, such as an image file, music file, spreadsheet, or letter.
Many languages treat our interactions, such as input and output, with programs, as a stream of bytes (always remember it is not bits). These bytes represent data that can be interpreted as representing values that we understand. This data comes in different forms.
Some examples are:
Character : O – A letter
Your Name : Root – A string of characters
Your age: 21 – A number often called Integer
Your phone number : 013473647367 – also a number but a very big one in that
Distance from your home to your favorite computer shop : 4.5 miles – Also a number but with a decimal fraction.
Within the program we process this data in various ways, such as adding them up, subtracting them from one another or sorting them in some manner.
Designing and creating programs are mostly centered around understanding the types of data we want to manipulate as well as how to manipulate that data.
Data types differ from language to language. But we may group the main data types under hierarchies. They must be one of the following:
The following diagram demonstrates this.
The most commonly supported types of data in programming languages are:
We can perform different operations depending on the type of data we are working on. For instance, we can calculate the root of integer 4, but the root of the string “hello world” does not make any sense.
In a nutshell, computer programs use data types to organize different types of data in a program. The data type determines the values stored on the memory and the operations that can be performed on it.
As we are learning Java let us discuss the data types in Java. In Java data types can be classified into two categories. They are,
- primitive type
- non-primitive type
A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share a state with other primitive values.
Java has eight primitive types:
double. Anything else is a non-primitive type.
Non-primitive type is out of the scope of this tutorial. We will only discuss the primitive type in this tutorial.
We have already discussed – the byte, short, int, and long data types. In addition to them, Java Programming Language supports four other primitive data types.
Primitive Data Types In Java
The eight primitive data types supported by the Java Programming Language are discussed in some detail in the following :
byte: A byte is an 8-bit signed two’s complement integer. It has a minimum of -128 and a maximum of 127 (inclusive). In large arrays, where memory savings are important, the byte data type can be useful for saving memory. They can also be used in place of int when their limitations help to clarify your code; the fact that a variable’s range is limited can serve as documentation.
short: 16-bit signed two’s complement integer is the short data type. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). The same rules apply as with byte: you can use a short to save memory in large arrays where the memory savings are significant.
int: The int data type is a 32-bit signed two’s complement integer with a minimum value of -231 and a maximum value of 231 , by default. The int data type can be used in Java SE 8 and later to represent an unsigned 32-bit integer with a minimum value of 0 and a maximum value of 232-1. To use the int data type as an unsigned integer, use the
Integer class. Static methods such as
divideUnsigned, and so on have been added to the Integer class to support unsigned integer arithmetic operations.
long: A 64-bit two’s complement integer is the long data type. The signed long has a value that ranges from -263 to 263 You can use the long data type in Java SE 8 and later to represent an unsigned 64-bit long with a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a larger range of values than int provides. To support unsigned long arithmetic operations, the
Long class includes methods such as
divideUnsigned and so on.
float: Float is a single-precision 32-bit IEEE 754 floating point data type. Its value range is beyond the scope of this discussion, but it is specified in the Java Language Specification’s Floating-Point Types, Formats, and Values section. If you need to save memory in large arrays of floating point numbers, use a float (rather than a double) as recommended for byte and short. Never use this data type for precise values, such as currency. You will need to use the java.math.BigDecimal class for this. Numbers and Strings cover BigDecimaland other useful Java platform classes.
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is beyond the scope of this discussion, but it is specified in the Java Language Specification’s Floating-Point Types, Formats, and Values section. This data type is usually the default choice for decimal values. As previously stated, this data type should never be used for precise values like currency.
boolean: True and false are the only possible values for the boolean data type. Use this data type to track true/false conditions with simple flags. This data type represents a single bit of information, but its “size” is not specified.
char: A single 16-bit Unicode character is represented by the char data type. It has a minimum of ‘u0000’ (or 0) and a maximum of ‘uffff’ (or 65,535 inclusive).
Do not worry if you do not understand all the jargon. Only remember that first 6 of the primitive data types represent numbers of different types, the seventh – boolean data type represents a logical type, and the last data type – char data type represents a single character.