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

1. Objective: Command Line Arguments in 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 Arguments In Java | Clone() Method In Java

2. Introduction of Java Command Line Arguments

Command Line Argument in Java for instance, “java Dog Shadow is a German shepherd” where the class, Dog is a class, while rest “Shadow is a German shepherd” is line argument. The command here is the Dog.

Clone() Method in Java – It creates a clone for an object i.e. an exact copy of the object. It helps to create an exact copy of the object with exactly the same contents which correspond to the original one.

Do you know the major difference between method overloading vs overriding in Java?

3. Command Line Arguments in Java

When command line arguments in Java are 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.

class Hello
{
   public static void main(String[] args)
   {
       if (args.length > 0)
       {
           System.out.println("The command line"+
                              " arguments are:");
           for (String val:args)
               System.out.println(val);
       }
       else
           System.out.println("No command line "+
                              "arguments found.");
   }
}
Java Quiz

4. 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 –

import java.io.*;
class Test
{
   int x, y;
   Test()
   {
       x = 10;
       y = 20;
   }
}
class Main
{
   public static void main(String[] args)
   {
        Test ob1 = new Test();
        System.out.println(ob1.x + " " + ob1.y);
        Test ob2 = ob1;
        ob2.x = 100;
        System.out.println(ob1.x+" "+ob1.y);
        System.out.println(ob2.x+" "+ob2.y);
   }
}

Output:
10 20
100 20
100 20

Let’s Discuss the battle of Java Vs C++.

5. Creating a Copy using Clone() Method

The class whose question’s duplicate is to be influenced must have a public clone technique in it or in one of its parent class.

  • Each class that actualizes clone() should call super.clone() to get the cloned object reference.
  • The class should likewise execute java.lang.Cloneable interface whose object clone we need to make, else it will toss CloneNotSupportedException when clone method is approached that class’ question. 

Syntax –

protected Object clone() throws CloneNotSupportedException

6. Usage of Clone() Method – Shallow Copy

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

import java.util.ArrayList;
class Test
{
   int x, y;
}
class Test2 implements Cloneable
{
   int a;
   int b;
   Test c = new Test();
   public Object clone() throws
                  CloneNotSupportedException
   {
       return super.clone();
   }
}
public class Main
{
   public static void main(String args[]) throws
                         CloneNotSupportedException
   {
      Test2 t1 = new Test2();
      t1.a = 10;
      t1.b = 20;
      t1.c.x = 30;
      t1.c.y = 40;
      Test2 t2 = (Test2)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);
   }
}

Output:
10 20 300 40
100 20 300 40

Hence, in the above illustration, t1.clone restores the shallow clone of the object t1. Basically, to acquire a profound clone of the question certain alterations must be made in clone strategy in the wake of getting the duplicate.

Let’s have a look at Java wildcard

a. Deep Copy Vs Shallow Copy

  • A shallow copy is a technique for replicating a question and is trailed a default in cloning. In this technique, the fields of an old object X are replicated to the new object Y. While duplicating the question write field, the reference is replicated to Y i.e. object Y will point to the same area as pointed out by X. In the event that the field value is a primitive sort, it duplicates the estimation of the primitive kind.
  • In this way, any progressions made in referenced objects in question X or Y will be reflected in other objects.

Shallow duplicates are modest and easy to make. In above cathe se, we made a shallow duplicate of the object.

7. Utilization of Clone Strategy – Deep Copy

  • On the off chance that we need to make a deep copy of object X and place it in another question Y, then a new duplicate of any referenced articles fields are made and these references are set in object Y. This implies any progressions made in referenced question fields in object X or Y will be reflected just in that question and not in the other.
  • A deep duplicate duplicates all fields and makes duplicates of powerfully apportioned memory indicated by the fields. A deep duplicate happens when a protest is replicated alongside the articles to which it alludes.
import java.util.ArrayList;
class Test
{
   int x, y;
}
class Test2 implements Cloneable
{
   int a, b;
   Test c = new Test();
   public Object clone() throws
               CloneNotSupportedException
   {
       Test2 t = (Test2)super.clone();
       t.c = new Test();
       return t;
   }
}
public class Main
{
   public static void main(String args[]) throws
                            CloneNotSupportedException
   {
      Test2 t1 = new Test2();
      t1.a = 10;
      t1.b = 20;
      t1.c.x = 30;
      t1.c.y = 40;
      Test2 t3 = (Test2)t1.clone();
      t3.a = 100;
      t3.c.x = 300;
      System.out.println(t1.a + " " + t1.b + " " +
                         t1.c.x + " " + t1.c.y);
      System.out.println(t3.a + " " + t3.b + " " +
                         t3.c.x + " " + t3.c.y);
   }
}

Output:
10 20 30 40
100 20 300 0

Let’s explore 5 Simple Steps – Establish JDBC Connection in Java

Hence, in the above case, we can see that another object for test class has been relegated to a duplicate question that will be returned in the Clone method. Due to this t3 will acquire a deep duplicate of the protest t1. So any progressions made in ‘c’ question fields by t3, won’t be reflected in t1.

8. Advantages of Clone Method

  • On the off chance that we utilize task administrator to appoint an object reference to another reference variable then it will point to same address area of the old object and no new duplicate of the object will be made. Because of this, any adjustments in reference variable will be reflected in the unique object.
  • On the off chance that we utilize duplicate constructor, at that point we need to duplicate the majority of the information over unequivocally i.e. we need to reassign every one of the fields of the class in constructor expressly. However, in Clone Method, this work of making another duplicate is finished by the method itself. So, to keep away from additional handling we utilize object cloning. 

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

9. 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.

For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.