(Java pointer), object reference problem

Source: Internet
Author: User
  1. Elicit pointers

On the face of it, Java has no pointers, or, in other words, pointers are weakened. But pointers are still true in Java. In Java we call this a reference.

String a;//Reference is empty

String a = new Srting ("");//content is empty

A points to a handle (two pointers), a method table and a class that points to the object, and a pointer to the object's data.

2. Cloning of objects

Problem:

string S1 = new String ("I am a sting");

String s2 = S1;

In fact, S1 and S2 point to the same object, and the variable holds a reference to the object.

Solve:

  1. Define Class XXX
  2. Implement the Cloneable interface in a class (The string class is not implemented), and implement the Clone () method
  3. Using the Clone () method in the calling class

XXX x1 = new xxx ();

xxx x2 = (XXX) x1.clone ();

(Java pointer), object reference problem

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.