My thinking in Java Study Notes (3)

Source: Internet
Author: User
Chapter 3 Control Procedure
In the Java World, objects and data are processed through operators, while selection and judgment are implemented by control statements.
Use Java Operators
The two functions of the operator. 1. The operator accepts arguments and generates new values, such as int A = 1 + 1. 2. The operator can also change the value of the operand, for example, I ++;
Most operators can work on the basic data type, but =, = ,! = Is an exception. They can act on any object.
Priority
I have a headache! Therefore, I have never remembered the priority of operators, but added parentheses in unclear places, so that not only do you seem very clear, but the error rate is also low, you can also see your code clearly.
Assignment and assignment
Assign a value to obtain the value on the right of the operator (any constant, variable, or expression that can generate a value) and pass the value to the left, use the = Operator because the basic data type stores actual values, not a handle (or a pointer in C ++), A = B; this is to pass the specific value of B to the variable A. When you change a, the value of B will not change with the change of, however, when you operate on objects, it is easy to see the opposite of the above phenomenon. When you operate an object, you actually operate on the object handle. When you assign an object to another object,
Actually assigned 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 ("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 );
}
}
In two, Class One is created with two objects and assigned 10 and 20 values. Of course, the output is 10 and 20, and then the O2 is assigned to O1. The output must be 20 and 20, but the wonderful thing was generated when the assignment was made for 3rd Times, when we changed the value of o1. I to 30, why did our o2. I change to 30? We didn't change his value! This is because both O1 and O2 contain the same handle. The handle stored in the original O1 is overwritten, and the address refers to the same memory space as O2, the memory space that he originally pointed to was reclaimed by the garbage collection mechanism when appropriate. In fact, this process happened in the 2nd assignments we thought there would be no problems! The above phenomenon is called the alias phenomenon, which is discussed in Appendix A of thinking in Java! If we don't want this to happen, we can use the o1. I = o2. I statement to perform Step 1. This method ensures that the two objects are independent of each other, however, this is not a good method, so that direct operations on the data in the object will lead to confusion and violate the idea of OOP. A better method is available in appendix.
Alias when calling a function
Class One
{
Int I = 10;
}
Class Two
{
Static void Hello (one o)
{
O. I = 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 will create an instance object named "O" one for us in the living space of the hello () function to accept the arguments passed by the outside world, however, the above method is actually just a someone handle, while hello () the changes to the I of the object named "O" in it will also affect the I in someone that is out of the Hello living space. It is really depressing!
Mathematical Operators
Here, we should pay attention to the problem that the result of the division operation of integers will ignore the fractional part. Note that it is not rounded down! What is a simple operator? A simple operator is followed by a = sign. This method is used by all operators in Java. For example, the value of X + 10 is assigned to X, you can simply use X + = 10. I will not give you an example of specific mathematical operators, because it is very simple and there is no difference with the mathematical operations we have learned in elementary school.
Unary operator
Negative signs (-) and positive signs (+) are all unary operators.
Increment and decrease
Increment (++) and decrease (--) both have two forms. One is the prefix (++ I) and the other is the suffix (I ++, the two methods have differences. When using the prefix, the prefix operation is performed first, and then the value is passed to I, And then I participates in subsequent operations, when a suffix is used, I is first involved in the operation, and then I is used in 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 perform auto-increment, then display the value (1) the second display is to first display the value of I (1), in auto-increment (2 ), however, the auto-increment is not displayed because the value of the auto-increment participates in the next operation, that is, the third display of 2.
Relational operators
Note that I have written an article on operators in my blog (http://blog.csdn.com/maoerzuozuo) about Operators
Frequent errors and answers. If you can get all the correct answers, you have no problem with the mathematical operators!
Logical operators
This is actually the most depressing part of me, because I can't remember what it means and or not.
Let's wait! I will go back to cram school this evening! I will post an article for you tomorrow ~ If you have any questions during your learning, contact me in this way.
E-mail molmd@163.net/163.com QQ: 31349283 we can learn together!
Welcome to my blog, http://blog.csdn.com/maoerzuozuo has more learning content!
 

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.