Monday, July 16, 2018

what is the code for shallow copy in java?

class Human {
    int age;
    long salary;
}
public class ShallowCopyExe {
    public static void main(String args[]) {
        Human h1 = new Human();
        h1.age = 20;
        h1.salary = 1000;
        System.out.println("h1 age: "+h1.age+", h1 salary: "+h1.salary);
        Human h2 = h1;
        h2.salary = 1200;
        System.out.println("h1 age: "+h1.age+", h1 salary: "+h1.salary);
        System.out.println("h2 age: "+h2.age+", h2 salary: "+h2.salary);
    }
}
Output:
h1 age: 20, h1 salary: 1000
h1 age: 20, h1 salary: 1200
h2 age: 20, h2 salary: 1200

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...