Java Program to construct one String from another


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements