Copy string java – Java Program to Copy a String

Copy string java: Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Java Program to Copy a String

  • Java program to find length of a sting using length method.

In this java program, to copy a string we will first ask user to enter a string and store it in an String object “str”. Then to copy a string we will create a new Object of String class by passing str to constructor method of String class. The new string object will contain the same set of character sequence as in original input string.
For Example,
Input String : Apple
Copied String : Apple

Java program to copy a string

Java copy string: In this java program, we first ask user to enter a string and then store it in String object “str”. Then we copy the input string by creating a new String object “copy” which is initialized with “str”.
Java program to copy a string

package com.tcc.java.programs;
 
import java.util.Scanner;
 
/**
 * Java Program to Copy a String
 */
public class CopyString {
    public static void main(String args[]) {
        String str, copy;
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Enter a String");
        str = scanner.nextLine();
        // Copying str string to copy string
        copy = new String(str);
 
        System.out.println("Input String : " + str);
        System.out.println("Copied String : " + copy);
    }
}

Output

Enter a String
Tech Crash Course
Input String : BTechGeeks
Copied String : BTechGeeks