What is Java String Literal



Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. The Java platform provides the String class to create and manipulate strings.

You can also create a String directly as ?

String greeting = "Hello world!";

A string literal should be enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello world!'.

Example

Live Demo

public class StringDemo {
   public static void main(String args[]) {
      String str = "Hello world!";
      System.out.println( str );
   }
}

Output

Hello world!
Updated on: 2022-09-05T11:34:39+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements