The Artima Developer Community
Sponsored Link

Java Buzz Forum
shallow copy java example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
shallow copy java example program Posted: Aug 14, 2016 1:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: shallow copy java example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • We create exact copy of the object using Cloneable in java.
  • We need to override object class clone() method.
  • This is also called clone of the object.
  • So we can create copy of object using two different way
    1. Shallow copy
    2. Depp copy

Shallow copy / Shallow cloning in java

  • In Shallow copy object reference will be copied instead of copying whole data of the object.
  • So the newly created object will be another reference for existing with same data means shallow copy of the object.
  • Whenever we change data in the original object same changes made to new object also because both are pointing to same object.

shallow copy in java


Program #1: Java example program to demonstrate shallow copy / shallow cloning



Example:
  1. package com.shallowcopyvsdeppcopy;

  2. public class Example{
  3. int a;
  4. int b;

  5. Example(int a, int b){
  6.     this.a=a;
  7.     this.b=b;
  8. }

  9. }


Empclone :

  1. package com.shallowcopyvsdeppcopy;

  2. public class Empclone implements Cloneable {

  3.     Example e;
  4.     int a;
  5.    
  6.     Empclone (int a, Example es){
  7.         this.a=a;
  8.         this.e=e;
  9.       
  10.     }
  11.    
  12.  public Object clone()throws CloneNotSupportedException{
  13.       
  14.         return super.clone();
  15.  }
  16.           
  17.    
  18. public static void main(String[] args) {
  19.     
  20.         Empclone a= new Empclone (2, new Example(3,3));
  21.         Empclone b=null;
  22.    
  23.         try {
  24.              b=(Empclone )a.clone();
  25.           
  26.         } catch (CloneNotSupportedException e) {
  27.             
  28.             e.printStackTrace();
  29.         }
  30.         System.out.println(a.e.a);
  31.         System.out.println(b.e.a);
  32.       
  33.         a.e.a=12;
  34.         System.out.println(a.e.a);
  35.         System.out.println(b.e.a);
  36.     }

  37. }

Output:


  1. 3
  2. 3
  3. 12
  4. 12

Read: shallow copy java example program

Topic: Best Deals of the Week, August 8-12 - Deal Alert Previous Topic   Next Topic Topic: Does my method look big in this?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use