GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — java-basics/core-java/methods/ShallowCopy.java at master · learning-zone/java-basics

Expand file tree

/

ShallowCopy.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (33 loc) · 1018 Bytes
/

ShallowCopy.java

Copy path

File metadata and controls

41 lines (33 loc) · 1018 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package methods;
/**
* Whenever we use default implementation of clone method we get shallow copy of
* object means it creates new instance and copies all the field of object to that
* new instance and returns it as object type, we need to explicitly cast it back
* to our original object. This is shallow copy of the object.
*
*/
import java.util.ArrayList;
class ShallowCopy1 {
int x, y;
}
class ShallowCopy2 implements Cloneable {
int a, b;
ShallowCopy1 c = new ShallowCopy1();
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class ShallowCopy {
public static void main(String[] args) throws CloneNotSupportedException {
ShallowCopy2 t1 = new ShallowCopy2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
ShallowCopy2 t2 = (ShallowCopy2)t1.clone();
t2.a = 100;
t2.c.x = 300;
System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
System.out.println(t2.a + " " + t2.b + " " + t2.c.x + " " +t2.c.y);
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.