Command Line Arguments in Java | Java Clone() Method

Today we are going to learn about command line arguments in java and java clone() method. Let’s start!!!

What are command line arguments in java?

In Java, the programmers can give the arguments via coding or during the run time. This is where the concept of command line argument emerges in java. The arguments here are passed as space-separated values. Both the String and primitive datatype, including int, double, float, char, etc., can be passed as command-line arguments.

When the JVM encounters command-line arguments, it is converted to a string array and passed to args[] in the main() function. The length of args is checked using args.length.

The command-line arguments are wrapped in order. Usually, the args[] array stores the first command-line argument in args[0], the following in args[1], the next in args[3], and so on.

Sample program using command-line arguments:

The following problem receives only one argument and prints it. Therefore, you are required to give one argument in the console.

class FirstCode{
public static void main(String args[]){
System.out.println(“The given argument is: ”+args[0]);
}
}

This program is executed in the following way:

Compiling: > javac FirstCode.java
Run: > java FirstCode knowledge

Output:

The given argument is: knowledge

Sample program that prints values using command-line arguments:

This program helps you print n number of arguments in the output:

class FirstCode{
public static void main(String args[]){
for(int=0; i<args.length;i++)
System.out.println(args[i]);
}
}  

The program is executed in the following way:

Compiling: javac FirstCode.java
Run: java FirstCode knowledge 1 2 3 study

Output:

knowledge
1
2
3
study

clone() method in Java:

In general terms, the clone is a copy of an object. It creates the same replica without any major differences. In Java, the clone() method does the same task. This method of the Object class clones an object to create another version of it. The class implements the Java.lang cloneable interface when it wants to create a clone for an object. When this is not done, the CloneNotSupportedException will be thrown.

Syntax:

protected Object clone() throws CloneNotSupportedException

Why is cloning preferred to copying in java?

Copying an object is a bit more complex than cloning it. The clone() method in Java saves a lot of processing time. This is why programmers use the clone() method instead of copying objects using the new keyword.

Advantages of Object cloning in java:

1. We can avoid using multiple lines of code. An abstract class with 4-5 lines of code using the clone() method would give the desired result.

2. When making changes in a previously developed project, the clone() method is the efficient way for copying objects. We can implement the Cloneable is a parent class to get it done.

3. We can also copy arrays using the clone() method fast.

Disadvantages of java object cloning:

1. This process requires changing the syntax a lot. We must implement the Cloneable interface, define clone() method, handle CloneNotSupportedException and call Object.clone().

2. When there are no other methods, the Cloneable interface must be implemented. To achieve this, we must direct the JVM to perform the clone() method in the given object.

3. And we must make the clone() method call the Object.clone() indirectly. This is because, the Object.clone() is usually protected.

4. The programmers are left with no control regarding the construction of an object as the Object.clone() does not invoke any constructor.

5. When a child class requires a clone() method, then it should be inherited in all its superclasses. Else, the flow of the super.clone() will eventually fail.

6. Finally, this Object.clone() allows only shallow copying. Any attempt to create a deep clone will require overriding.

Sample program to demonstrate Java clone() method:

Class FirstCode implements Cloneable{
String word;
int number; 
public static void main(String args[]){
FirstCode fc1 = new FirstCode();
fc1.word = “Java”;
fc1.number = 23;
System.out.println(fc1.word);
System.out.println(fc1.number);
try{
FirstCode fc2 = (FirstCode)fc2.clone();
System.out.println(fc2.word);
System.out.println(fc2.number);
}
catch(Exception e){
System.out.println(e);
}
}
}

Output:

Java
23
Java
23

Sample program to change value using java clone() method:

Class FirstCode implements Cloneable{
String word;
int number; 
public static void main(String args[]){
FirstCode fc1 = new FirstCode();
fc1.word = “Java”;
fc1.number = 23;
System.out.println(fc1.word);
System.out.println(fc1.number);
try{
FirstCode fc2 = (FirstCode)fc2.clone();
System.out.println(fc2.word);
System.out.println(fc2.number);

fc2.word = “Python”;
System.out.println(fc2.word);
System.out.println(fc1.word);
}
catch(Exception e){
System.out.println(e);
}
}
}

Output:

Java
23
Java
23

Shallow copy vs Deep copy in java:

Shallow copy in java:

  • It is a default method that copies the object.
  • The reference of an object is copied to the newly created object.
  • If we make any changes in the referenced objects, it is likely to reflect in the other object.
  • It is easier and simple to create when compared to deep copy.

Sample program to clone using java shallow copy:

public class FirstCode{
public static void main(String args[]){
first f = new first();
second s = (first) f.clone();
f.buff.append(“with FirstCode”);
System.out.println(f);
System.out.println(s);
}
}
Class first implements Cloneable{
public StringBuffer buff = new StringBuffer(“Learn Java”);
public String toString(){
return buff.toString();
}
public Object clone(){
try{
return super.clone();
}
catch(CloneNotSupporttedException e){
}
return null;
}
}

Output:

Learn Java with FirstCode
Learn Java with FirstCode

Java Deep copy:

  • It requires overriding the clone() method.
  • It is not a default method.
  • This copies every field and created copies of dynamically allocated memory pointed to the fields.
  • It usually takes place when an object is copied with the other objects that it is referred to.

Sample program to clone using deep copy in java:

public class FirstCode{
public static void main(String args[]){
first f = new first();
second s = (first) f.clone();
f.buff.append(“with FirstCode”);
System.out.println(f);
System.out.println(s);
}
}
Class first implements Cloneable{
public StringBuffer buff = new StringBuffer(“Learn Java”);
public String toString(){
return buff.toString();
}
public Object clone(){
try{
first f = (first) super.clone();
second.buff =  new StringBuffer(buff.toString());
return f;
}
catch(CloneNotSupporttedException e){
}
return null;
}
}

Output:

Learn Java with FirstCode
Learn Java

Conclusion:

This was all about command line arguments in java and java clone() method. As the clone() method copies the value of an object to another, the desired output is achieved. This is comparatively better for creating objects using the new keyword. Therefore, writing multiple lines of code can be reduced with the help of the clone() method.

Leave a Reply

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