In this example, let us see how to initialize String in Java.
Source Code
package com.beginner.examples;
public class StringInitializationExample {
public static void main(String a[]){
//String Initialization
String str1 = "java";
System.out.println(str1);
String str2 = new String("example");
System.out.println(str2);
String str3 = str1 + " example";
System.out.println(str3);
char[] s = {'j', 'a', 'v', 'a'};
String str4 = new String(s);
System.out.println(str4);
}
}
Output:
java
example
java example
java