FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-basics/core-java/methods/ShallowCopy.java at master · CodeForHunger/java-basics · GitHub
CodeForHunger
/
java-basics
Public
forked from
learning-zone/java-basics
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-basics
/
core-java
/
methods
/
ShallowCopy.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (33 loc) · 1018 Bytes
Breadcrumbs
java-basics
/
core-java
/
methods
/
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
);
}
}
Back
|
FazBrowse Home
|
New Git URL