Thinking in java learning tie (3)

xiaoxiao2021-03-06  105

Chapter III Control Process Process In the world of Java, the processing of objects and data is through operators, while selecting and determining are implemented by control statements. Two uses of the Java operator operator: 1, the operator accepts the quotes, and generates a new value, such as INT A = 1 1; 2, the operator can also change the value of the operand, such as i ; most operators It is possible to act on top of the basic data type, but =, == ,! = Yes, they can act on any object. Priority, this is actually very headache! So I have never remember what operator's priority, add parentheses in the unclear place, not only you look very clear, nothing, the error rate is also low, others come to see your code can be clear. The meaning of assignment and assignment is the value of the right side of the operator (any constant, variable, can generate a value of the expression), and pass the value to the left, using the operator = number because the basic data type is stored actual Numerical, non-handle (can also be understood as a pointer in C ), a = b; this sentence is passed to a variable of this specific value, when you change A, B This value does not follow you A changes to change, but when you operate, it is easy to appear and the above phenomenon. When you actually operate, it is actually an object's handle. When you assign an object to another object, it actually assigns a handle, for example: Class One {Int i = 0;} Class TWO {Public static void main (string [] args) {one O1 = new one (); one O2 = new one (); O1.i = 10; o2.i = 20; system.out.print ("O1.i = " O1.i); // 10 System.out.println (" o2.i = " o2.i); // 20 O1 = O2; System.out.Print (" O1.i = " O1 .i); // 20 System.out.println ("o2.i =" o2.I); // 20 O1.I = 30; system.out.print ("o1.i =" O1.i ); // 30 system.out.println ("O2.i =" O2.I); // 30}} Class One is created in Two, and is assigned to 10 and 20, respectively.

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 is in the appropriate time will be garbage 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 of ​​OOP, and a better method is in Appendix A. The alias problem when calling the function Class One {INT 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); somene.i);}} Our Java program creates a name for us within the survival space of the Hello () function O ONE instance object, used to accept the quotes delivered in the outside world, but we use the above way, it is only just a Someone handle, and the name within Hello is called an O object. The changes to i will also affect the i in someone outside the Hello survival space, it is really depressed! Mathematical operators Here we have to pay attention to a problem is that the result of the integer division operation will ignore the decimal part, pay attention to it is not all round! What is a brief operator? The brief operator is followed by an operator followed by a = number, this method 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.

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

New Post(0)