String literal in java

Let’s learn string literal in java.

String literal in java

String literal is a sequence of unicode characters like numbers, symbols and letters.

String str = “#15, 86d street, Brooklyn”;

These characters are enclosed between double quotation mark. Javac compiler converts all string literal characters into unicode code points.

Also read – nested classes in java

We can use escape sequence like this,

\n – newline

\t – inserting tab

\b – backspace

\r – carriage return

\’ – single quotation mark

\\ – backslash

\” – double quotation mark

\f – form feed

\d – octal

\xd – hexadecimal

\ud – Unicode character


Now let’s a java program on string literals,

// java string literals
public class StringLiteralDemo 
{
   public static void main(String[] args) 
   {
      String str = "Hello World Java";
      System.out.println(str);
      String name = "ViratKohli";
      System.out.println(name);
      String message = "Hey Sachin, \"How are you?\"";
      System.out.println(message);
      System.out.println("Hii, \n Welcome to flower brackets blog\n");
      String welcome = "Earn while you sleep\u2122";
      System.out.println(welcome);
   }
}



Output:

Hello World Java
ViratKohli
Hey Sachin, “How are you?”
Hii,
Welcome to flower brackets blog

Earn while you sleep™