Variables and Data Types
One of the fundamental concepts in computer programming is memory, used to store information in the computer. Computers use bits as the smallest information that can be stored. A bit is either a 1 or 0. We can group 8 bits to get what is called a byte. Because bits are very small, we usually deal with bytes as the smallest unit when programming. When we write programs, what we are essentially doing is fetching some bits from a certain memory location, doing some operations on them, and writing back the result to a memory location.
We need a way to store different kinds of data in the computer's memory and tell the computer what kind of data is stored at what memory location.
Data types are a way for us to specify what kind of data and the size we need to store at a given memory location. An example of a data type is an integer, a character, or a string. Broadly, the data types available in Java can be classified into the following types:
- Primitive data types
- Reference data types
Primitive types are the fundamental types, that is, they cannot be modified. They are indivisible and form the basis for forming complex types. There are eight primitive data types in Java, which we will cover in depth in the subsequent sections:
- byte
- short
- int
- long
- char
- float
- double
- boolean
Reference types are types that refer to data that's stored in a certain memory location. They don't hold the data themselves, but hold the address of the data. Objects, which will be covered later, are examples of reference types:

Figure 2.1: Representation of reference types
All data types have the following common properties:
- They are associated with a value.
- They support certain operations on the value they hold.
- They occupy a given number of bits in memory.
For example, an integer can have a value such as 100, support operations such as addition and subtraction, and is represented using 32-bits on the computer's memory.
Variables
Whenever we want to deal with a given data type, we have to create a variable of that data type. For example, to create an integer that holds your age, you would use a line like the following:
int age;
Here, we are saying the variable is called age and is an integer. Integers can only hold values in the range -2,147,483,648 to 2,147,483,647. Trying to hold a value outside the range will result in an error. We can then assign a value to the age variable, as follows:
age = 30;
The age variable now holds the value 30. The word age is called an identifier and is used to refer to the memory location where the value 30 is stored. An identifier is a human-readable word that is used to refer to the memory address of the value.
You can use a word of your choice as an identifier to refer to the same memory address. For example, we could have written this as follows:
int myAge ;
myAge = 30;
Here is a graphical representation of the preceding code snippet:

Figure 2.2: Representation of age in memory address
As much as we can use any word as an identifier, Java has some rules on what makes up a valid identifier. The following are some of the rules to adhere to when creating identifier names:
- Identifiers should start with either a letter, _, or $. They cannot start with a number.
- Identifiers can only contain valid unicode characters and numbers.
- Identifiers cannot have spaces in between them.
- Identifiers can be of any length.
- Identifiers cannot be reserved keywords.
- Identifiers cannot have arithmetic symbols such as + or -.
- Identifiers are case-sensitive, for example, age and Age are not the same identifiers.
Reserved Keywords
Java also contains inbuilt words that are reserved and cannot be used as identifiers. These words have special meanings in the language.
Now let's discuss the primitive data types in Java. As we said before, Java has 8 primitive data types, which we will look at in detail.
- Fundamentals of Linux
- Mobile Web Performance Optimization
- Docker and Kubernetes for Java Developers
- C#程序設計實訓指導書
- R語言游戲數據分析與挖掘
- Oracle BAM 11gR1 Handbook
- 低代碼平臺開發實踐:基于React
- 用戶體驗可視化指南
- 好好學Java:從零基礎到項目實戰
- Java語言程序設計教程
- C# 10核心技術指南
- 計算機視覺實戰:基于TensorFlow 2
- Learning Ext JS(Fourth Edition)
- React Router Quick Start Guide
- 大學計算機基礎