Java alias problem

Source: Internet
Author: User

"Alias" means that multiple handles try to point to the same object, as shown in the previous example. If someone writes something to that object, it creates an alias problem. If the owner of the other handle does not want that object to change, I am afraid it will be disappointed. This can be illustrated by the following simple example:

: Alias1.java
//aliasing two handles to one object public

class ALIAS1 {
  int i;
  ALIAS1 (int ii) {i = II}
  public static void Main (string[] args) {
    Alias1 x = new ALIAS1 (7);
    Alias1 y = x; Assign the handle
    System.out.println ("x:" + x.i);
    System.out.println ("y:" + y.i);
    System.out.println ("incrementing X");
    x.i++;
    System.out.println ("x:" + x.i);
    System.out.println ("y:" + y.i);
  }
///:~

For the following line:
Alias1 y = x; Assign the handle
It creates a new ALIAS1 handle, but not assigns it to a fresh object created by new, but instead assigns it to an existing handle. So the contents of the handle X-that is, the address that object X points to-are assigned to Y, so both x and Y are connected to the same object. This way, once X's I add value in the following statement:
x.i++;
The I value of Y is also bound to be affected. From the final output you can see:

X:7
y:7
incrementing x
x:8
y:8

The most straightforward solution at this point is simply not to do this: do not intentionally point multiple handles to the same object in the same scope. Doing so makes your code easier to understand and debug. However, once you are ready to pass the handle as an argument or parameter--this is the normal way that Java envisions it--the alias problem automatically occurs because the local handle created may modify the external object (objects created outside the scope of the method). Here is an example:

: Alias2.java
//method calls implicitly alias their
//arguments.

public class Alias2 {
  int i;
  ALIAS2 (int ii) {i = II}
  static void F (Alias2 handle) {
    handle.i++;
  }
  public static void Main (string[] args) {
    Alias2 x = new ALIAS2 (7);
    System.out.println ("x:" + x.i);
    SYSTEM.OUT.PRINTLN ("Calling F (x)");
    f (x);
    System.out.println ("x:" + x.i);
  }
///:~

The output is as follows:
X:7
Calling f (x)
X:8

Method changes its own argument--the external object. Once this happens, it is important to determine whether it is reasonable, whether the user is willing to do so, and if it is causing problems.
Typically, we call a method to produce a return value, or to change the state of the object for which the method is invoked (the method is actually the way we "send a message" to that object). It is rarely necessary to call a method to handle its arguments, which is called the "side effect" of the Use Method (Side Effect). So if you create a method that modifies your own parameters, you must explicitly point out the situation to the user and warn about the possible consequences of using that method and its potential threats. Because of these confusions and flaws, we should try to avoid changing the parameters.
If you need to modify a parameter during a method call and do not intend to modify the external parameter, you should make a copy inside your own method to protect that parameter. Most of the content of this chapter revolves around this issue.

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.