Thinking Logic of computer programs (1)

Source: Internet
Author: User


The procedure is probably going on.


The computer is a machine, the machine is mainly composed of CPU, memory, hard disk and input. Computers running on the operating system, such as Windows or Linux, run various applications on the operating system, such as word, QQ, etc.


The operating system divides time into a lot of small time slices, a time slice for one program, another time slice for another program, and frequent switching between programs. However, in the application's view, the entire machine resource seems to be his own, and the operating system gives him the illusion.


For programmers, we write programs, basically do not consider other applications, we think well how to do their own things.


The application seems to be able to do a lot of things, can read and write documents, can play music, can chat, can play games, can go ...


But in essence, computers only perform pre-written instructions, and these instructions are just manipulating data or devices. The so-called program, basically tells the computer to manipulate the data and the execution of the sequence of instructions, that is, what data to do what operation.


For example:


    • Reading the document is loading the data from the disk into memory and then outputting it to the monitor

    • Writing a document is to write data back to disk from memory.

    • Playing music is to load the music data into memory and then write it to the sound card.

    • Chat, is to receive chat data from the keyboard, put into memory, and then passed to the network card, through the internet to another person's network card, and then from the card to the memory, display on the display.


Basically, all data needs to be put into memory for processing, and a large part of the program's work is to manipulate the data in memory.


This article is mainly about this "data". (in Java, for example)


Data


What is the data? Data inside the computer is binary, inconvenient operation, in order to facilitate the operation of data, high-level language introduced the "Data type" and "variable" concept.


Data Type


Data types are used to categorize data, facilitate understanding and manipulation, and, for the Java language, have the following basic data types:


    • Integer type: There are four types of integer byte/short/int/long, each with a different range of values

    • Decimal type: There are two types of float/double, with different ranges of values and precision

    • Character type: Char, representing a single character

    • True or False type: Boolean, True or False


Basic data types have corresponding array types, and arrays represent multiple records of the same data type with fixed lengths, which are stored together in memory.


For example, a natural number can be represented by an integer-type data, and 100 consecutive natural numbers can be represented by an array of integers with a length of 100. A character is represented by a char, and a paragraph of text can be represented by a char array.


Java is an object-oriented language, in addition to the basic data types, the other is the object type, what is the object exactly?


Simply put, an object is a combination of basic data types, arrays, and other objects to make it easier to manipulate it as a whole.


For example, a student object can consist of the following information:


    • Name: a character array

    • Age: An integer

    • Gender: one character

    • Admission Score: One decimal


The date is also an object in Java, which is represented internally as a shaped long.


Just as the composition of all things in the world consists of 108 basic elements in the periodic table, the basic data types are equivalent to the basic elements of chemistry, and the objects are equivalent to all things in the world.


variables


In order to manipulate the data, the data needs to be stored in memory, the so-called existence of the program appears to be an address number of contiguous space, placed in the memory of a location, in order to conveniently find and manipulate this data, need to give this location a name. The programming language uses the concept of variables to represent this process.


Declaring a variable, such as int A is actually allocating a space in memory, this space holds the int data type, a points to the location of this memory space, by operation A to the memory space of a point, such as a=5 This operation can be a point to the memory space to change the value of 5.


The variable is called because it represents an in-memory location, and the value stored in this location can vary.


Although the value of the variable can be changed, but the name is unchanged, the name should represent the programmer in the mind of the memory location of the meaning, the meaning should be constant, for example, the variable int second represents the clock seconds, at different times can be given different values, but it represents the clock seconds. It should be because it is not necessary, if you have to take a variable named age but give it the height of the value, the computer also take you no way.


The important words again! variable is to give the data name, easy to find different data, its value can change, but the meaning is not strain. Another example is a contract that can have four variables:


* First_party: Meaning is party a

* Second_party: Meaning is party b

* Contract_body: Meaning is the content of the contract

* Contract_sign_date: Meaning is the date of contract signing


The meanings of these variables are determined, but their values are different for different contracts.


Novice programmers often use meaningless names like A,b,c,hehe,haha to give variables a meaningful name!


By declaring variables, each variable giving a data type and a meaningful name, we tell the computer what data we want to manipulate.


With the data, how to manipulate the data?


-long serial, not to be continued, please pay attention. Author of the public number ( Lao Ma said programming ), welcome attention, communication.

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7E/42/wKiom1b6et6DtXkgAABn5hBROlo105.jpg "title=" QRCode _for_gh_7bb71a4b27cd_258.jpg "alt=" Wkiom1b6et6dtxkgaabn5hbrolo105.jpg "/>


This article from "Lao Ma said Programming" blog, reproduced please contact the author!

Thinking Logic of computer programs (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.