Java object reference Processing Mechanism

Source: Internet
Author: User

What is the alias reference mechanism of Java? The alias reference mechanism of Java (originally Aliasing, alias, namely polymorphism in Java) means that multiple referenced variables can be located on the same physical object, the referenced variables can be of different types. in the following code, the S Class inherits the P class, And the pp and ss are the two array variable names of the P and S types respectively. [java] public class TestPolyMorphism {public static class P {public void print (String message) {System. out. println ("P -->" + message) ;}} public static class S extends P {public void print (String message) {System. out. println ("S -->" + message) ;}} public static void m Ain (String [] args) {S [] ss = new S [10]; P [] pp = ss; ss [0] = new S (); pp [0]. print ("hello ");//!! Runtime error. The parent class object cannot be assigned to the subclass array; // pp [1] = new P (); // java. lang. arrayStoreException} in the memory, both pp and ss point to the same memory address. (we can say that the pointer pp, pointer ss; or pp reference, ss reference .) figure 1 PP and ss point to the same physical address. during actual operation, the polymorphism feature determines whether to call a specific method of parent class or subclass based on the actual object type, instead of referencing the variable type. how does Java handle the alias reference mechanism? If the following code is copied to eclipse, there will be no compilation errors: [java] package think; public class TestPolyMorphism {public static class P {public void print (String message) {System. out. println ("P -->" + message) ;}} pub Lic static class S extends P {public void print (String message) {System. out. println ("S -->" + message);} public void paint (String message) {System. out. println ("S --> execute some painting operations -->" + message) ;}} public static void main (String [] args) {S [] arr = new S [10]; P [] pp = arr; arr [0] = new S (); pp [0]. print ("hello"); pp [1] = new P ();//!! Runtime error. The parent class object cannot be assigned to the subclass array; java. lang. arrayStoreException} but the following error is displayed at runtime: [plain] Exception in thread "main" java. lang. arrayStoreException: think. testPolyMorphism $ P at think. testPolyMorphism. main (TestPolyMorphism. java: 22) the reason is that the alias reference is processed during Java runtime. During the program running, the VM finds that the first element of the array pp is an SS-type object, not a PP-type object. Therefore, only the correct code can run properly: [java] S [] arr = new S [10]; P [] pp = arr; pp [1] = new S (); pp [1]. print ("hello"); no output error: [plain] view plaincopy view the CODE piece on the CODE to derive to my CODE piece S --> hello

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.