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

  • Java Fundamentals
  • Gazihan Alankus Rogério Theodoro de Brito Basheer Ahamed Fazal Vinicius Isola Miles Obare
  • 684字
  • 2021-06-11 13:37:55

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.

主站蜘蛛池模板: 三明市| 连南| 五河县| 灵山县| 金昌市| 肇庆市| 淅川县| 阳西县| 丹江口市| 永清县| 杂多县| 施秉县| 高唐县| 托克托县| 沙湾县| 巨野县| 麻阳| 定远县| 大厂| 古蔺县| 沁源县| 游戏| 曲阜市| 元氏县| 五河县| 冷水江市| 时尚| 灌云县| 兴山县| 小金县| 搜索| 通辽市| 霍林郭勒市| 苍山县| 湘乡市| 石楼县| 临沭县| 肥东县| 宜春市| 偃师市| 吉林市|