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

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.

主站蜘蛛池模板: 北票市| 江津市| 鄂托克前旗| 林周县| 嘉黎县| 鄄城县| 高青县| 灌南县| 纳雍县| 兴国县| 油尖旺区| 新闻| 沂水县| 额尔古纳市| 江津市| 彭阳县| 长汀县| 罗江县| 北辰区| 顺昌县| 建始县| 云安县| 同心县| 昭通市| 丰都县| 确山县| 江陵县| 彭水| 虎林市| 光山县| 苏州市| 新巴尔虎左旗| 通化县| 玛沁县| 兴安县| 定州市| 同心县| 十堰市| 甘德县| 当涂县| 阿克陶县|