Site icon DataFlair

Command Line Arguments In Java | Clone() Method In Java

command line arguments in Java

command line arguments in Java

Free Java courses with 37 real-time projects - Learn Java

In our last chapter, we talked about How to Read Java Console Input. Now, we are ready to discuss Command Line Arguments in Java Programming. In addition, we will learn the clone() method in Java and deep copy and shallow copy. At last, we study the advantages of clone methods.

So, let’s start Command Line Arguments in Java.

Command Line Arguments in Java

Command line argument is mainly used to control your program from outside. When a command is sent to the JVM, it wraps them and sends them to args[]. The args[] array here actually wraps the arguments by checking the length of the argument using args.length.

Clone() Method in Java

No operator is meant to create a copy of the object in Java which is unlike C++. In Java, we use the assignment operator to create a copy of the variable and not the object.
Example –

package com.dataflair.commandlineargument;
class Test
{
  int variable1, variable2;
  Test()
  {
    variable1 = 10;
    variable2 = 20;
  }
}
public class CloneMethodInJava 
{
  public static void main(String[] args)
  {
    Test ob1 = new Test();
    System.out.println(ob1.variable1 + " " + ob1.variable2);
    Test ob2 = ob1;
    ob2.variable1 = 100;
    System.out.println(ob1.variable1+" "+ob1.variable2);
    System.out.println(ob2.variable1+" "+ob2.variable2);
  }
}

Output:

Creating a Copy using Clone() Method

The class must have a public clone method in it or in one of its parent class.

Syntax –

protected Object clone() throws CloneNotSupportedException

Usage of Clone() Method – Shallow Copy

Let’s learn Shallow Copy with the help of an example-

package com.dataflair.commandlineargument;
class Test1
{
  int variable1, variable2;
}
class Test2 implements Cloneable
{
  int testVar1;
  int testVar2;
  Test1 testObject = new Test1();
  public Object clone() throws
  CloneNotSupportedException
  {
    return super.clone();
  }
}
public class CloneMethodShallowCopy {
  public static void main(String args[]) throws
  CloneNotSupportedException
  {
    Test2 testObject1 = new Test2();
    testObject1.testVar1 = 10;
    testObject1.testVar2 = 20;
    testObject1.testObject.variable1 = 30;
    testObject1.testObject.variable2 = 40;
    Test2 testObject2 = (Test2)testObject1.clone();
    testObject2.testVar1 = 100;
    testObject2.testObject.variable1 = 300;
System.out.println(testObject1.testVar1 + " " + testObject1.testVar2 + " " +
    testObject1.testObject.variable1 + " " + testObject1.testObject.variable2);
System.out.println(testObject2.testVar1 + " " + testObject2.testVar2 + " " +
    testObject2.testObject.variable1 + " " + testObject2.testObject.variable2);
  }
}

Output:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Hence, in the above illustration, t1.clone restores the shallow clone of the object t1. Basically, to get a deep clone of the objects certain alterations must be made in clone method for getting the duplicate.

Deep Copy Vs Shallow Copy

Usage of Clone Method – Deep Copy

package com.dataflair.commandlineargument;
class Demo
{
  int variable1, variable2;
}
class Demo1 implements Cloneable
{
  int testVar1, testVar2;
  Demo testVar3 = new Demo();
  public Object clone() throws
  CloneNotSupportedException
  {
    Demo1 t = (Demo1)super.clone();
    t.testVar3 = new Demo();
    return t;
  }
}

public class CloneMethodDeepCopy {

  public static void main(String args[]) throws
  CloneNotSupportedException
  {
    Demo1 demoObj1 = new Demo1();
    demoObj1.testVar1 = 10;
    demoObj1.testVar2 = 20;
    demoObj1.testVar3.variable1 = 30;
    demoObj1.testVar3.variable2 = 40;
    Demo1 demoObj2 = (Demo1)demoObj1.clone();
    demoObj2.testVar1 = 100;
    demoObj2.testVar3.variable1 = 300;
    System.out.println(demoObj1.testVar1 + " " + demoObj1.testVar2 + " " +
        demoObj1.testVar3.variable1 + " " + demoObj1.testVar3.variable2);
    System.out.println(demoObj2.testVar1 + " " + demoObj2.testVar2 + " " +
        demoObj2.testVar3.variable1 + " " + demoObj2.testVar3.variable2);
  }
}

Output:

Advantages of Clone Method

So, this was all about the tutorial, Command Line Arguments in Java and Clone Method in Java. Hope you like our explanation

Conclusion

Hence, in this tutorial of Command Line Arguments in Java and Clone Method in Java, we have a complete understanding of Command Line Arguments in Java and about the clone() method in Java. Moreover, we saw creating a copy using clone() method with syntax and examples. In addition, we discussed usage of Clone method as Shallow Copy and Deep Copy. At last, we saw the advantages of Clone Method. Furthermore, if you have any query in Command Line Arguments and Clone method, you can ask in the comment section.

Exit mobile version