How do programmers spend Valentine's Day gracefully?

It's Valentine's day again, and couples will start a vigorous show of love again.

For programmers, "single" is still the biggest Bug in life. The key is that they can't debug yet.

Although the saying goes "heroes are lonely since ancient times", life without another half is always incomplete. So, how do programmers spend this year's nue Festival gracefully?

01 normal Edition

A cup of tea, a pack of cigarettes, a bug for a day.

02 Advanced Edition

Valentine's Day? How can the festival of the circle of friends lose?

Ultimate Edition 03

What is Valentine's day? The product will go online tomorrow

You must have a lot of single dog s, right?

No problem! Da Xiong teaches you six ways to create objects, from low-end to high-end. There is always one suitable for you. If you don't have an object, new one!

6 ways to create objects

Suppose there is a girlfriend class:

Method 1: give yourself a new object

No object, we'll just new one ourselves!

Yes, using the new keyword is also the easiest and direct way for Java to create objects.

/**
 * new An object
 */
@Test
public void girlFriend1() {
    GirlFriend girlFriend = new GirlFriend("new An object");
    System.out.println(girlFriend);
}

Output result:

GirlFriend(name=new an object)

Method 2: clone an object

Others have objects, but you don't. If you can, clone someone else's girlfriend?

Let the girlfriend class first implement the clonable interface and its clone() method:

/**
 * Girlfriend class
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
class GirlFriend implements Cloneable {

   private String name;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
}

Note: shallow copy is used by default in this demonstration, that is, only basic type fields are cloned. For reference types, the clone() method needs to be rewritten to manually assign the value of the reference field.

Now clone an object:

@Test
public void girlFriend2() throws CloneNotSupportedException {
    GirlFriend girlFriend1 = new GirlFriend("Clone an object");
    GirlFriend girlFriend2 = (GirlFriend) girlFriend1.clone();
    System.out.println(girlFriend2);
}

Output result:

GirlFriend(name = clone an object)

The advantage of using cloning is that you can quickly create an object with the same value as the original object. The field value of the object is the same, but there are two different references.

Method 3: class sends an object (reflection)

Directly use girlfriend to distribute an object:

/**
 * Class to dispatch an object
 */
@Test
public void girlFriend3() throws InstantiationException, IllegalAccessException {
    GirlFriend girlFriend = GirlFriend.class.newInstance();
    girlFriend.setName("Class to dispatch an object");
    System.out.println(girlFriend);
}

Output result:

GirlFriend(name = class sends an object)

If you know where the girlfriend class is (the full path of the class), but it is not loaded, reflect an object:

/**
 * Reflect an object
 */
@Test
public void girlFriend4() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    GirlFriend girlFriend = (GirlFriend) Class.forName("cn.javastack.test.jdk.core.GirlFriend").newInstance();
    girlFriend.setName("Reflect an object");
    System.out.println(girlFriend);
}

Output result:

GirlFriend(name = reflect an object)

Method 5: construct an object (reflection)

Knowing the construction of girlfriend class, you can call the constructor to construct an object:

/**
 * Construct an object
 */
@Test
public void girlFriend5() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
    GirlFriend girlFriend = GirlFriend.class.getConstructor().newInstance();
    girlFriend.setName("Construct an object");
    System.out.println(girlFriend);
}

Output result:

GirlFriend(name = construct an object)

Method 6: deserialize an object

This is similar to the function of cloning. If a girlfriend was serialized (saved) on disk, it can be deserialized now.

First, make your girlfriend Serializable and implement the Serializable interface:

/**
 * Girlfriend class
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
class GirlFriend implements Cloneable, Serializable {

    private static final long serialVersionUID = 1L;
    
    private String name;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

Serialization / deserialization object example code:

/**
 * Deserialize an object
 */
@Test
public void girlFriend6() throws IOException, ClassNotFoundException {
    GirlFriend girlFriend1 = new GirlFriend("Deserialize an object");

    // I have a girlfriend
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("gf.obj"));
    objectOutputStream.writeObject(girlFriend1);
    objectOutputStream.close();

    // Deserialize
    ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("gf.obj"));
    GirlFriend girlFriend2 = (GirlFriend) objectInputStream.readObject();
    objectInputStream.close();

    System.out.println(girlFriend2);
}

Output result:

GirlFriend(name = deserialize an object)

If you don't like using code to generate objects for yourself, you can also try the programmer's unique romantic expression.

Although many people say that programmers don't understand romance and can only type code, no wonder they don't have girlfriends. But! Da Xiong wants to say, promise me to find a boyfriend and find a programmer!

Added by the-hardy-kid on Sat, 19 Feb 2022 07:42:46 +0200