官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 安泽县| 横山县| 平谷区| 诸城市| 融水| 宝坻区| 大城县| 尚义县| 庐江县| 乐清市| 定州市| 师宗县| 谢通门县| 手游| 霍州市| 板桥市| 盐津县| 邵阳县| 兰坪| 通江县| 获嘉县| 德阳市| 东乡县| 泸西县| 社旗县| 洛南县| 西盟| 汝南县| 富源县| 顺平县| 泰来县| 盐津县| 彭阳县| 遂昌县| 文登市| 西乌| 通化县| 资阳市| 日土县| 民丰县| 甘谷县|