Java object reference processing mechanism __java

Source: Internet
Author: User
Tags static class java reference

Translation staff: Anchor

Translation time: November 13, 2013

SOURCE Link: How does Java handle aliasing?


What is Java's reference alias mechanism The Java Reference alias mechanism (aliasing, alias, or polymorphism in Java) means that multiple reference variables can be positioned to the same actual physical object, which 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 type P and S type respectively.

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 Main (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 memory, both PP and SS point to the same memory address. (We can say, pointer pp, pointer ss; or PP reference, SS reference.)

Fig. 1 pp and SS point to the same physical address, in which the polymorphic attribute determines the specific method of calling the parent class or subclass based on the actual object type, rather than the type of the reference variable.

how Java handles the referral alias mechanism
If you copy the following code into eclipse, there will be no compile-time errors:
package;


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
    void Paint (String message) {
        System.out.println ("s--> performs 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 (); // !! Run-time error, unable to assign parent class object to subclass array; java.lang.ArrayStoreException
    }
}

However, the following error will be displayed at run time:
Exception in thread "main" Java.lang.ArrayStoreException:think. Testpolymorphism$p at
    . Testpolymorphism.main (testpolymorphism.java:22)
Because Java handles alias references at runtime, the virtual machine discovers that the first element of the array pp is the SS-type object, not the PP type, during the program's operation.
As a result, you can work correctly only if you modify it to the correct code:
s[] arr = new S[10];
p[] pp = arr;
PP[1] = new S ();
Pp[1].print ("Hello");
The output has no errors:
s-->, hello.

Related articles:
1. Linux Process programming–fork ()
2. Illustrated java--understand the most popular 8 images of Java mechanism
3. Why do we need Generic Types in Java?
4. Overriding and overloading in Java with examples

Related Article

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.