Thinking in Java Learning Notes (3) (ZT)

xiaoxiao2021-03-06  101

?

Chapter 3 ?? Control Process Process

In the world of Java, the processing of objects and data is through operators, while choices and judgments are implemented by control statements.

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Use the Java operator ????? 2 use, 1, operator accept the quotes, and generate new values, such as INT A = 1 1; 2, the operator can also change the value of the operand, such as i ;

????? Most operators can act on top of the basic data type, but =, == ,! = Yes, they can act on any object.

?????

Priority order

??????? This is actually very headache! So I will never remember what operator's priority, plus parentheses in can't figure out, not only you look very clear, nothing is low, others can see your code is also clear.

?????

Assignment and assignment

??????? The meaning of assigning value is to obtain the value of the operator (any constant, variable, can generate a value of the expression), pass the value to the left, using the operator = because the basic data type is stored It is the actual value, not the handle (you can also understand the pointer in me C ), a = b; this sentence is passed to a variable of this specific value, when you change A, the value does not follow Change your changes, but when you operate the object, it is easy to appear and the opposite of the above phenomenon. When you actually operate, it is actually an object's handle, when you assign an object to another object,

??????? actually assign a handle, for example

??

??????? class one ????????? {?????????? I = 0; ?????????} ?????? ??? Class TWO ?????????????plic static void main (string args []) ??????????? {?? ??????????? one O1 = new one (); ????????????? one O2 = new one (); ?????????? ??? o1.i = 10; ?????????????? o2.i = 20; ????????????? system.out.print ("O2. i = " O1.i); ?????????????? system.out.println (" o2.i = " o2.i); ?????????? ??? o1 = o2; ????????????? system.out.print ("o2.i =" O1.i); ???????????? ? System.out.println ("o2.i =" o2.i); ?????????????? o1.i = 30; ???????????? ? System.out.print ("o2.i =" o1.i); ????????????? system.out.println ("o2.i =" o2.i); ????????????} ?????????} ??????? Class ONE is created in TWO, and is assigned to 10 and 20 separately. Output is of course 10 and 20, then assign O2 to O1, the output is definitely 20 and 20, but the wonderful thing is generated when the value is assigned, when we change O1.i to 30, discovery Why is our O2.i become 30? We didn't change his value! This is because O1 and O2 contain the same handle. The original O1 stored handle is overwritten, the address refers to the same memory space as O2, and the memory space he originally pointed back is in an appropriate time. The recycling mechanism is for recycling. In fact, this process happens in the second assignment we don't have problems! The above phenomena is called alias, this is a special topic in Appendix A of Thinking in Java! If we don't want to happen this, we can use O1.i = O2.i to perform step 2, such a way of writing can ensure that the two objects are independent of each other, but this is not a good way to operate directly. Data within the object can cause confusion, and the idea of ​​OOP is contrary to the idea, and better methods are in Appendix A. Alias ​​problem when calling a function

??????????? class one ???????????????? I = 10; ???????? ???} ??????????? Class Two ??????????? {???????????? static? Void Hello (One O)? ??????????? {?????????????? oi = 20; ????????????} ???????? ???? public static void main (string args []) ????????????????? one someone = new one (); ?? ??????????? system.out.println (someone.i); ????????????? hello (someone); ?????????? ??? system.out.println (someone.i); ??????????????????????????}

??????????? Our Java program creates an instance object of one name that is an O. of the list of ONE, which is used to accept the quotes passing in the outside world. We use the above way, and it is only a Someone handle, and the name of the name of the O object of Hello () will also affect the I of Someone outside the Hello living space. It's really depressed! ??????

? Mathematical operator

????????? Here we must pay attention to a problem is that the result of the division of the integer will ignore the decimal part, be careful not to be rounded! What is a brief operator? A brief operator is followed by an operator followed by a = number, which is used in all operators in Java. For example, the value of X 10 is given from the new assignment to x, you can use X = 10 simple form. Example of the specific mathematics operator, I don't give you, because this is very simple, and there is no difference in mathematics operations in our primary school.

??????????

One yuan operator ??????????? negative number (-) and positive ( ) are one yuan operator

???????

Increment and decrementing ????????? Increment ( ) and decrement (-) They all have 2 forms, one is a prefix ( i), one is a suffix (i ) need to pay attention, These two methods are different. When using prefix, it is the previous prefix, and then passes the value to i, then participate in the back of the operation, and use the suffix, first put the I to participate in the operation, then The operation of the suffix, for example

???????

?? public class test ???????????? public static void main (String args []) ?????????? {???? ??????? INT i = 0; ??????????? system.out.println ( i); ??????????? system.out.println (i ); ??????????? system.out.println (i); ??????????} ?????? ???? The answer is 1, 1, 2 first, then display the value (1) The second display is to display the value of i first (1), in the self-add (2), but it is not Display because the self-added value participates in the next operation, it is the reason for the third display 2.

???????

Relationship operator ????????? Note, I have written an article about the operator in my blog (

Http://blog.9cbs.com/maoerzuozuo) About operators

???????? Regular place and answer, if you can answer all, then you are basically no problem in mathematical operators!

转载请注明原文地址:https://www.9cbs.com/read-123374.html

New Post(0)