Java programming thought notes--assignment

Source: Internet
Author: User

The assignment uses the operator "=". It means "take the right value (i.e. the right value) and copy it to the left (that is, the left value)". The right value can be any constant, variable, or expression (as long as it can generate a value on the line). But the lvalue must be a definite, named variable. That is, you must have a physical space to store the value to the right of the equals sign.

I. Assignment of basic data types

The assignment to the base data type is simple. The base data type stores the actual numeric value, not the reference to an object, so when you assign it, you copy the contents of one place directly to a different place.

   Public class testa{      publicstaticvoid  main (string[] args) {        int b;        b=9;        A=b;        System.out.println ("1.a=" +a+ "   " + "b=" +b);          A=10;        System.out.println ("2.a=" +a+ "   " + "b=" +b);         /* * Output Result: *1.a=9  b=9 *2.a=10  * /

The code above, using A=b for the base data type, copies the contents of B to a. If a is subsequently modified, B is not affected by this modification at all. However, when you assign a value to an object, the situation is different.

Second, the "assignment" of the object

When an object is assigned an operation, we really manipulate the object's reference. So assigning an object to another object is actually copying the reference from one place to another. This means that if you use C=d for objects, then both C and D point to the object that was originally only D.

 Public classPerson {intAge ;} Public classTESTB { Public Static voidMain (string[] args) {person P1=NewPerson (); Person P2=NewPerson (); P1.age=10; P2.age=20; P1=P2; System.out.println ("1.p1.age=" +p1.age+ "p2.age=" +p2.age); P1.age=15; System.out.println ("2.p1.age=" +p1.age+ "p2.age=" +p2.age); }}/** Output Result: * 1.p1.age=20 p2.age=20* 2.p1.age=15 p2.age=15*/

The above code, which uses P1=P2 for the person object and then assigns a value to the P1, also changes the P2. This is because P1 and P2 contain the same references, which point to the same object. The original P1 contains a reference to the object, which is a pointer to an object with a value of 10. When assigning a value to a P1, the reference is overwritten, which is lost, and the object that is no longer referenced is automatically cleaned up by the garbage collector.

Java programming thought notes--assignment

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.