Java Program to construct one String from another

Java 8Object Oriented ProgrammingProgramming

To construct on string from another, firstly take a charcter array for the first string.

char ch[] = { 'A', 'M', 'I', 'T' };
String str1 = new String(ch);

The above forms first string. Now, let us created another string from the first string.

String str2 = new String(str1);

In this way, we can easily construct one string from another.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      char ch[] = { 'A', 'M', 'I', 'T' };
      String str1 = new String(ch);
      String str2 = new String(str1);
      String str3 = new String(str2);
      System.out.println("String 1: "+str1);
      System.out.println("String 2: "+str2);
      System.out.println("String 3: "+str3);
   }
}

Output

String 1: AMIT
String 2: AMIT
String 3: AMIT
raja
Published on 14-Dec-2018 06:47:04
Advertisements