I taught her to write code in those days 3 java language foundation

She seems to be really determined to learn to write programs, and since bought the book later, almost every day there is spare time, she will be holding a book, although the reading at the same time, his brow is furrowed. I am very understanding, maybe there is no entry for people who write textbooks really is the bible of the program in general, as new to me as when, by the time a full semester came to understand the writing process is doing. To see her this way, I seem to be happy, so she just needs me, because I have more reason to stay with her.

"Dai!" When I go to the bathroom passing her station, she stopped me, "you tell me about it, I really do not understand this ah!"

Her anxious look, and I was really a little anxious, because I hold back for a moment, on hand written code only go to the bathroom.

"When I come on a toilet." I threw such a sentence, hurriedly away.

When I came back from the toilet, just met her hope in their eyes as well as a series of questioning.

"What is the identifier? Why do not key identifier? What is a data type? What is a variable? Operator is the arithmetic sign it? ......"

"1 plus 1 equal to a few?" Before she finished, I asked.

"Equals 2 ah." She Lengzheng a moment, then said, and looked at me with bewildered eyes.

"Yes, it is equal to 2. If we allow that program to count it, how to write a program?"

"I do not know." She was very straight answer.

"Try it." I pointed to her laptop, "according to your ideas a try."

She opened the computer, and then create a text file, and then into a .txt extension .java, I see such a move some happy, she remembers java source files for the program are based on .java extension of . But behind the thing, in my unexpected and expected. She opened the file, and then enter in it:

1+1

"That's it?" I asked.

"Yes." She said, then she added, "I know I do not write, I said I do not know how to write the thing."

"Okay." I said, "But you did not find all of the sample code on the books all have a main method of it?"

She looked at me blankly, I know she did not know what I'm saying, so I can find an example code in the book to her.

"See no? Like this, you have to remember that all programs are main entry method, that program from your main method begins execution."

She is still a loss, but I can not explain more, so I took a pen and drew two red frame in her book, then said: "Well, first you remember, all the code is written in red square between those two portions circled box on the line. or write code that must be written on the two parts. "

. "Good," she said, putting her just write code changed:

"So?" She asked tentatively.

"Ah, no, there is one thing you have to remember that each statement java program is a semicolon at the end of the English character."

"Oh," she said, immediately behind that 1 + 1 added a semicolon:

"This is just great." She said.

"Not yet." I said.

"It is not right?" She asked.

"No, just do not ask you what is the variable you, now you need to play the variables." I said, "you think about it, where you do increase over future results put it?"

"Oh, I know, it is the variable to put the results." She was a little excited and her sudden insight. I also cheer up, because she's happy.

"Yes, the variable is used to put the data." I said, she began to change the code because I know that although she seemed to understand what variables are, but still helpless on the front of the code:

Of course, I wrote the code of output results, in order to see the results:

With the result, she was happy, then excitedly into the 1 + 1 + 1 2,3-4 and other experiments, but fortunately the computer is not a fool, all output the correct result. Suddenly she has a gum tree because she wanted to try multiplication and division, but there is no primary school textbooks that multiplication and division key on the keyboard.

"There is no multiplication and division key on the keyboard, how to perform multiplication and division ah?" She looked at me with eyes saying for help.

"Some ah." I said.

"Where is it?" She puzzled.

"Here," I pointed and * / says on the keyboard.

"Oh, no wonder the Scripture says, operators there appear the two symbols, multiplication and division turned out to be ah."

Then she went to test ride divide and multiply. Multiplication is no problem, they must enter the correct result, but the division has a problem. When she calculated 3/2, the result turned out to be an output.

"Computer a problem?" She was puzzled silly computer.

"Computer is not wrong, you're wrong." I am a little amused, because it is almost common sense for an old programmer, of course, is not for her, so I expect them to laugh, immediately attracted her eyes.

"What are you laughing? Is 3 divided by 2 equals 1.5 should not do?" She said.

"1.5 is right. But the variables you used to keep the results are wrong, you are using an integer variable, that can only store integer, you use it to store decimal, he would automatically decimal thrown away. this is the type of data, different data types need different types of variables exist, otherwise there will be data loss problems. so ...... "

"So here we use a variable of type decimal to keep." I had finished, she was excited to take over my words.

"Yeah, right, then change what program it."

"How to change it?" She asked.

"Storage decimal data type in Java for two, one double, a float." I said, turn to the book explain the basic data types, where to her, "There are details."

Types of

Do not type

byte

Ranges

byte

Integer

1byte

-27 ~ 27-1

short

Integer

2Replacement

-215 ~ 215-1

int

Integer

4byte

-231 ~ 231-1

long

Integer

8byte

-263 ~ 263-1

float

Float

4byte

3.402823e+38 ~ 1.401298e-45

double

Float

8byte

1.797693e + 308 ~ 4.9000000e-324

char

Text type

2Replacement

0~216-1

boolean

Boolean

1byte

true/false

 "Ah, I read here, but these two have what difference does it make?" She asked.

"The difference like different sizes, like a bucket, filled with different sizes of data, smaller float type variable loading of data, data loaded double bigger." I drew an analogy I want her to understand a little better.

"Well, probably understand." She said, changed under the code.

"So?" She looked back at me and said uncertainty.

"Try not to know." I said.

So she compiled to run, but the result was unexpected.

 "Or not ah." She cried, "Is it really my computer is broken?"

I feel funny, is not everyone began to learn to write a program like this, the computer will suspect a problem yet.

"Ha ha," I said with a smile, "the computer is not bad, just here to use the type conversion."

"In fact, the calculation of the computer is that, is the integer 3, 2 is an integer, it finds are integers, the result of speculation that it should also be an integer, so it put the results into an invisible integer variables inside, that is, put a 1 in the hidden variables. "I said, pointing to the equal sign," I did not see the equal sign, that is the assignment operator, is to copy data from one variable to another variable. here we put the computer to calculate the results of just one copy of the stealth variable to variable a, and understand not, here we output a variable value inside, of course, is 1, and also because a variable is of type float, all 1.0 is the. "Having said that I have a large section, I do not know it to understand a few percent. But she apparently assign this new concept became interested in listening to my long-winded explanation of a pass, she asked: "? Why should we be an assignment operator"

"Because we take the result is stored up for later use In fact, many times the program is to be delivered to a variety of data moved, which requires containers to store the data. Put a little deeper, is the data in memory and each kind of external memory operation moved to go, but for now you no matter what the memory. "

"Well, now how do?"

"This easy to handle, with a mandatory conversion, the 2 or 3, as long as one becomes a decimal, the computer knows if the result of a decimal." Then, I changed the code, this time as scheduled expected, the correct output 1.5.

 

 "Oh, this is the cast, the type is preceded by a bracketed on it. I've seen in the book, but do not know where." She has to get new skills and to cheer up .

I think that through this example, she should have mastered the basic concepts of these variables, data types, operators, so I said: "? That I do not have now is not the best answer you began to make a series of questions that it"

She thought and said:. "Ah, I am concerned about my understanding, do not you think I'm right."

"Good." I said.

"1, variable data storage;

2, there is a variable type, you can store different types of data size;

3, different types of variables can be converted, the conversion process may lose a portion of the data, just as water is poured into the vat as kegs, if the capacity exceeds the keg, it will lose the excess water;

4, the operator is the arithmetic operation represented by these symbols;

Yes, there is assigned an equal sign, it said it would copy the right value to the variable on the left. "

She summed up a 123, it makes me a bit surprised, but then they take for granted is the case, because she had been reading Well, in fact, many of the concepts she already understood that this is their own understanding of uncertainty right. In this regard, I can not help showing approving look.

"Ah, there, is the name of the variable identifiers are not you?" She asked.

"So to speak, but not just the name of the variable, all the names are collectively referred to as identifiers, later you will find that we define a class, write a method which requires a name." I replied, "So, the key You know what the word is yet? "

"Ah, the keyword identifier is to be used."

"Yes, you are very savvy, very suitable for programmers." I have some ridicule impressed.

She was happy, his face filled with a proud smile.

"Now we do a more complicated topic point of it." I want to let her take the heat blacksmith of consolidating what you have learned today.

"Well." She replied, his face still smiling, seemed now to write any program here.

"Then I will go out topic oh."

"You said."

"Grade 1 school has two classes, a class has 31 students, b class has 35 students, and now you want to pull these two classes of students doing calisthenics on the playground, if seven people in a row, the question here, a few rows 1 can whole row, the number 2 if there is surplus can not make up the whole row, if the remaining number, that is how many people remaining? "

"This simple." She confidently, "31 + 35 equals 66, then 66 divided by 7 equals more than 9 3. have the answer, you can row entire row of nine, the remaining three individuals can not make up the whole row."

'I do not call you count ah, I let you write the program, let the computer count. "

"Well, I'll try."

So, the next 10 minute, her attention on her computer, but I admire her because the United States comes out of focus.

"Written." She looked up to me, I suddenly felt a little embarrassed, hurriedly moved her eyes from her computer screen.

"Yes." I said, "I will ask the."

"Of course, I am not stupid, these are not all written in the book you have." She said, pointing to the table book operators introduced to me.

priority

Operators

Brief introduction

Associativity

1

[ ] . ( )

The method calls, property acquisition

From left to right

2

!、~、 ++、 --

Unary operators

From right to left

3

* 、/ 、%

Multiplication, division, modulo (remainder)

From left to right

4

+ 、 -

Addition and Subtraction

From left to right

5

<<、 >>、 >>>

Left shift, right shift, unsigned right shift

From left to right

6

< 、<= 、>、 >=、 instanceof

Less than, less than or equal, greater than, greater than or equal,

Determining whether the object type of the same type

From left to right

7

== 、!=

Two values ​​are equal, the value is not equal to 2.

Below are detailed explanations

From left to right

8

&

Bitwise AND

From left to right

9

^

Bitwise XOR

From left to right

10

|

Bitwise or

From left to right

11

&&

Short-circuit and

From left to right

12

||

Short or

From left to right

13

?:

Conditional operator

From right to left

14

=、 += 、-= 、*= 、/=、 %=、 &=、 |=、 ^=、 <、<= 、>、>= 、>>=

Mixing assignment operator

From right to left

 

She was very proud, because she found she read the contents of the book, but I poured cold water on outdated:. "Good is good, but not perfect."

"That's how to write?" She received a smile on her face.

I feel out of my my straight male cancer, but I wanted her to learn more, so I do not Guannameduo, I started to change up her code.

Compile run:

Read the code I turn over, she exclaimed: "Such variables can also be used to calculate??"

"Of course, but necessary, since you'll find the program basically calculated variables. You do not find, where I spend the remainder of step calculated to compare and 0 is not a formula than you want to write a bunch much easier. "

. "Oh, I remember," she says, suddenly asked: "The priority is not just former child learned math, addition, subtraction, when considered together first multiplication and division, addition and subtraction so the first count, then we should brackets? "

"Well, quite right, we rarely have to remember the general priority of each operator, first count anyway, are bracketed just fine."

"Haha, what are you teaching students lazy, oh, oh you that the teacher is not qualified."

"Oh." I laughed, did not say anything.

 

 

Released four original articles · won praise 5 · Views 263

Guess you like

Origin blog.csdn.net/m0_46455711/article/details/105190314