Thinking logic of computer programs (1), thinking Logic
What is the program?
A computer is a machine that consists of CPU, memory, hard disk, and input/output devices. Operating systems run on the computer, such as Windows or Linux, and various applications, such as Word and QQ, run on the operating system.
The operating system divides the time into many small time slices. One time slice is used by one program, and the other time slice is used by another program, and the time slice is frequently switched between programs. However, in the application's view, the entire machine resource seems to be made by him, and the operating system provides him with this illusion. For programmers, if we write programs, we don't have to worry about other applications. We just need to think about how to do our own thing.
The application seems to be able to do many things, read and write documents, play music, chat, play games, and play go ...... but in essence, computers only execute pre-written commands, which only operate on data or devices. The so-called program basically tells the computer the data to be operated and the sequence of executed commands, that is, what data is to be operated on.
For example:
- Reading a document is to load data from the disk to the memory and then output it to the display.
- Writing a document is to write data back to the disk from the memory.
- Playing music is to load the music data to the memory and then write it to the sound card.
- Chat is to receive chat data from the keyboard, put it into the memory, then pass it to the NIC, pass it to another person's Nic through the network, then transfer it from the NIC to the memory, and display it on the monitor.
Basically, all data needs to be stored in the memory for processing. A large part of the work of the program is to operate on the data in the memory.
This article mainly talks about this "data ". (Take Java as an example)
Data
What is data? Data is binary in the computer, which is inconvenient to operate. To facilitate data operations, the advanced language introduces the concepts of "data type" and "variable.
Data Type
Data types are used to classify data for easy understanding and operation. For Java language, there are the following basic data types:
- Integer type: There are four types of integer byte/short/int/long, with different values
- Decimal type: float/double type, with different values and precision
- Character Type: char, indicating a single character
- True/false type: boolean, indicating true/false
Basic data types all have corresponding array types. arrays represent multiple records of the same data type with a fixed length, which are stored together in the memory. For example, a natural number can be represented by an integer, and 100 consecutive natural numbers can be represented by an integer array with a length of 100. A character is represented by a char, and a piece of text can be represented by a char array.
Java is an object-oriented language. Except for basic data types, all others are object types. What is an object? Simply put, an object is a combination of basic data types, arrays, and other objects to facilitate the overall operation.
For example, a student object may consist of the following information:
- Name: A character array
- Age: an integer
- Gender: one character
- Admission score: one decimal point
The date is also an object in Java, Which is internally expressed as an integer long.
Just like the composition of all things in the world, they are composed of 108 basic elements in the periodic table of elements. The basic data type is equivalent to the basic elements in chemistry, and the object is equivalent to everything in the world.
Variable
To operate the data, you need to store the data in the memory. The so-called internal program seems to be a continuous space with address numbers. After it is placed in a certain location in the memory, in order to conveniently find and operate the data, you need to name the location. Programming Language passedVariableThis concept represents this process.
Declare a variable. For example, int a actually allocates a space in the memory, which stores the int data type, and a points to the location of the memory space, you can perform operations on a to operate the memory space pointed to by a. For example, if a = 5, you can change the value of the memory space pointed to by a to 5.
The reason isChangeBecause it represents the location in the memory, the value stored in this location can be changed.
Although the value of the variable can be changed, the name remains unchanged. This name should represent the meaning of the memory location in the programmer's mind. This meaning should remain unchanged, for example, the int second variable indicates the number of seconds of the clock. Different values can be assigned at different times, but it indicates the number of seconds of the clock. Why?ShouldThis is because it is not necessary. If you have to set a variable named age but assign it a height value, the computer will not be able to help you.
If it is important, repeat it again! A variable is used to name the data so that you can find different data. Its value can be changed, but its meaning does not change. For example, a contract can have four variables:
- First_party: Party
- Second_party: Party B
- Contract_body: Meaning the contract content
- Contract_sign_date: indicates the contract signing date.
The meanings of these variables are definite, but their values are different for different contracts.
Beginners often use meaningless names like a, B, c, hehe, and haha to give a meaningful name to the variable!
By declaring variables, each variable is assigned a data type and a meaningful name, which tells the computer the data we want to operate on.
How can I operate on data with data?
------------------
Looking at the latest articles, please pay attention to the Public Account "lauma says programming" to explore the essence of Java programming and computer technology. Original article, retain all copyrights.