One of the advantages of the Java language is that it removes the notion of pointers, but it also causes many programmers to ignore the distinction between objects and references in programming, and this article tries to clarify this concept. And since Java cannot solve the problem of object duplication through simple assignment, in the development process, the Clone () method is often used to replicate objects. This article will let you know what is Shadow Clone and deep clone, understand their differences, advantages and disadvantages.
See this title, is not a bit confusing: the Java language clearly illustrates the cancellation of pointers, because pointers are often convenient and cause the source of code insecurity, but also make the program becomes very complex and difficult to understand, the misuse of pointers written code is no less than the use of the already notorious "GOTO" statement. The notion of Java abandoning pointers is absolutely sensible. But there is no explicit pointer definition in the Java language, and virtually every new statement returns a reference to a pointer, but most of the time Java does not care about how to manipulate the pointer, much less the fear of manipulating C + + pointers. The only thing to be more concerned about is passing objects to a function. The following routine:
Package Reference
Class obj{
String str = "init value";
Public String toString () {
return str;
}
public class objref{
obj aobj = new obj ();
int aint = 11;
public void Changeobj (OBJ inobj) {
Inobj.str = "changed value";
public void Changepri (int inint) {
Inint =;
public static void Main (string[] args)
{
ObjRef oref = new ObjRef ();
System.out.println ("Before Call Changeobj () method:" + oref.aobj);
Oref.changeobj (oref.aobj);
System.out.println ("after called Changeobj () method:" + oref.aobj);
System.out.println ("==================print primtive=================");
System.out.println ("Before Call Changepri () method:" + oref.aint);
Oref.changepri (Oref.aint);
System.out.println ("after called Changepri () method:" + oref.aint);
}
}
/* RUN result
before call Changeobj () Method:init value
after call Changeobj () method:changed VAlue
==================print primtive=================
before call Changepri () Method:11
after call Changepri () method:11
*
*/
The main part of this code calls two very similar methods, Changeobj () and Changepri (). The only difference is that they take the object as an input parameter, and the other takes the basic type int in Java as an input parameter. And the input parameters are changed inside both function bodies. Seemingly the same way, the results of the program output are not the same. The Changeobj () method actually changes the input parameters, while the Changepri () method does not change any of the input parameters.
From this example, we know that Java is different from the processing of objects and basic data types. As with C, when the basic data type of Java (such as int,char,double, etc.) is passed to the function body as the entry parameter, the incoming parameter becomes a local variable inside the function body, and the local variable is a copy of the input parameter. All functions within the body of the operation is for this copy of the operation, after the completion of the function, the local variable also completes its mission, it does not affect the input parameters as a variable. Parameter passing in this way is referred to as "value delivery." In Java, the passing of an object as an entry parameter defaults to "Reference pass", which means that only a "reference" to the object is passed, the concept of which is the same as a pointer reference in the C language. When an input variable is changed within the function body, it is essentially a direct manipulation of the object.
In addition to "reference passing" when a function passes a value, it is "reference pass" whenever a value is assigned to an object variable with "=". Such as:
package reference;
class PassObj
{
String str = "init value";
}
public class ObjPassvalue
{
public static void main(String[] args)
{
PassObj objA = new PassObj();
PassObj objB = objA;
objA.str = "changed in objA";
System.out.println("Print objB.str value: " + objB.str);
}
}
/* RUN RESULT
Print objB.str value: changed in objA
*/
The first sentence is to generate a new Passobj object in memory, and then assign the Passobj reference to the variable Obja, and the second sentence is to assign the reference of Passobj object to the variable OBJB. At this point Obja and OBJB are two identical variables, and any subsequent change to Obja is tantamount to a change in OBJB.
Even if you understand the concept of "pointer" in the Java language, you may inadvertently make the following mistake.