JAVA object value and reference

JAVA programming ideas, the main study object and the relationship between references recently.

In memory, references are mainly stored in the stack, while JAVA objects are mainly stored in the heap.

It is convenient to manage storage allocation and cleanup in the stack, and the compiler does not need to know the life cycle of the data in the heap, and the garbage collection mechanism of the JVM is used for unified management.

 

The basic type does not create a reference, but saves it directly in the stack, which is more efficient; but its encapsulation class must be able to create a reference.

At the same time, the concept of automatic boxing and automatic unboxing of basic types is introduced here, as follows:

Integer a = new Integer(8);
Integer b = new Integer(8);
int c = 8;
int d = 8;
System.out.println(a == b);//false;
System.out.println(a == c);//true;
System.out.println(d == c);//true;

 

So how to compare the object a and the object b of the above code, it should pass

 

a.equals(b);//true


to compare.

This is because the encapsulation class Integer of int overrides the comparison method of equals.

If it is a custom class and does not override the equals method

public class JDKTest {

    /**
     * Function description: <br>
     * <Function detailed description>
     *
     * @param args
     * @see [related class/method] (optional)
     * @since [product/module version] (optional)
     */
    public static void main(String[] args) {
        Value v1 = new Value();
        Value v2 = new Value();
        v1.i = v2.i = 100;
        System.out.println(v1.equals(v2));//false;
    }

}

class Value{
    int i;
}

After overriding the equals method

public class JDKTest {

    /**
     * Function description: <br>
     * <Function detailed description>
     *
     * @param args
     * @see [related class/method] (optional)
     * @since [product/module version] (optional)
     */
    public static void main(String[] args) {
        Value v1 = new Value();
        Value v2 = new Value();
        v1.i = v2.i = 100;
        System.out.println(v1.equals(v2));//true;
    }

}

class Value{
    int i;
    @Override
    public boolean equals(Object arg0) {
        if(this == arg0){
            return true;
        }
        if(! (arg0 instanceof Value) ){
            return false;
        }
        else if(this.i == ((Value)arg0).i){
            return true;
        }
        else {
            return false;
        }
    }
}


 


 

 

 

At the same time, when the parameter is passed, if it is not a basic type, it is also a reference.

public class JDKTest {

    static void f(Letter y){
        yc = 'Z';
    }
    
    /**
     * Function description: <br>
     * <Function detailed description>
     *
     * @param args
     * @see [related class/method] (optional)
     * @since [product/module version] (optional)
     */
    public static void main(String[] args) {
      Letter x = new Letter();
      x.c = 'a';
      System.out.println(x.c);

      f(x);
      System.out.println(x.c);
        
    }

}

class Letter{
    char c;
}

 

The result of the output:

a
Z

All are references, and the values ​​of the objects in the heap are updated, and the values ​​of all references to this object will change (because they are just "references").

 

If you need to pass an object instead of passing a reference to the object, you need to renew a new object and assign the value in the original object to the new object.

Letter x = new Letter();
x.c = 'a';
Letter y = new Letter();
yc = xc; // pass the value this way


Replenish:

I searched and found that there are already tool classes that can implement this function (the jar package commons-beanutils-1.8.3.jar is required)

org.apache.commons.beanutils.BeanUtils.copyProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException;

 

T t;
        try {
            t = clazz.newInstance();
            for (Field field : fields) {
                List<String> fieldValues = map.get(field.getName());
                if (fieldValues != null && fieldValues.size() > 0) {
                    if(field.getType().getName().equals("java.math.BigDecimal")){
                        if(!"".equals(fieldValues.get(0))){
                            BeanUtils.copyProperty(t, field.getName(), fieldValues.get(0));
                        }
                    }else {
                        BeanUtils.copyProperty(t, field.getName(), fieldValues.get(0));
                    }
                }
            }

        } catch (Exception e) {
            return null;
        }
        return t;


The following content is transferred from someone else's blog: The use of apache.commons.beanutils.BeanUtils

 

This class provides a series of static methods to operate the existing Java Class that conforms to the JavaBean specification. The JavaBean specification emphasized here is simply that a Java Class exposes its internal member variables to the outside world through a series of getter and setter methods (property). Through the static methods of BeanUtils, we can:

  • Copy an instance of JavaBean--BeanUtils.cloneBean();
  • Copy properties between two instances of a JavaBean -- BeanUtils.copyProperties(), BeanUtils.copyProperty();
  • Set member variable (property) values ​​for an instance of JavaBean--BeanUtils.populate(), BeanUtils.setProperty();
  • Read the value of member variables (properties) from a JavaBean instance -- BeanUtils.getArrayProperty(), BeanUtils.getIndexedProperty(), BeanUtils.getMappedProperty(), BeanUtils.getNestedProperty(), BeanUtils.getSimpleProperty(), BeanUtils. getProperty(),BeanUtils.describe();

In general, the BeanUtils class provides two types of functions: reading and writing member variables.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326075719&siteId=291194637