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

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...