|
A string literal is a representation of a value of a String object. They are represented as a sequence of characters enclosed in double quotes. Following are some examples of string literals:
String s = "SampleString"; //valid
printMessage("HelloWorld!"); // valid String literal in a method call
String s = 'SampleString'; //invalid
System.out.println("Hello" + " World!"); // valid, two string literals
You may have noticed that the literals we have studied so far were the primitive values. The Java strings are objects of the String class and are not a primitive datatype, yet they can be used as literals. The strings, enclosed in double quotes, are directly typed inside the Java code. The other non-primitive type that has a literal representation is an array, which we’ll look at in the next section.