Java Examples
Java Examples
memu home questions

String Example

This example demonstrates how to declare and initialize a String in Java.

Source: (

class StringExample {
  public static void main(String args[]) {
    // creating a String literal
    String str1 = "This is the first string.";
    // create a new String Object 
    String str2 = new String("This is the 2nd string.");
    System.out.println("str1: " + str1 );
    System.out.println("str2: " + str2 );


$ java StringExample
str1: This is the first string.
str2: This is the 2nd string.

Java Docs:

Questions answered by this page:

How to Declare and Initialize a String in Java? How do you initialize a String in java?

Related Examples: