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.
- Responsive Web Design by Example
- Oracle JDeveloper 11gR2 Cookbook
- Hands-On Automation Testing with Java for Beginners
- Unity 5 for Android Essentials
- Flutter跨平臺開發(fā)入門與實戰(zhàn)
- Python全棧數(shù)據(jù)工程師養(yǎng)成攻略(視頻講解版)
- HTML5+CSS3+jQuery Mobile APP與移動網(wǎng)站設計從入門到精通
- 從零開始學Android開發(fā)
- 遠方:兩位持續(xù)創(chuàng)業(yè)者的點滴思考
- Google Adsense優(yōu)化實戰(zhàn)
- Spring Boot從入門到實戰(zhàn)
- 軟技能2:軟件開發(fā)者職業(yè)生涯指南
- iOS應用逆向工程:分析與實戰(zhàn)
- Test-Driven Java Development(Second Edition)
- Java無難事:詳解Java編程核心思想與技術(第2版)