String type and Literals

The String class represents character strings.

A quoted string constant can be assigned to a String variable.

public class Main{
   public static void main(String[] argv){
      String str = "this is a test"; 

The output:

this is a test

String literals in Java are specified by enclosing a sequence of characters between a pair of double quotes.

The escape sequences are used to enter impossible-to-enter-directly strings.

"\"" is for the double-quote character.

"\n" for the newline string.

For octal notation, use the backslash followed by the three-digit number. For example, "\141" is the letter "a".

For hexadecimal, you enter a backslash-u (\u), then exactly four hexadecimal digits. For example, "\u0061" is the ISO-Latin-1 "a" because the top byte is zero. "\ua432" is a Japanese Katakana character.

Escape SequenceDescription
\dddOctal character (ddd)
\uxxxxHexadecimal Unicode character (xxxx)
\'Single quote
\"Double quote
\rCarriage return
\nNew line
\fForm feed

Examples of string literals are

"Hello World" 
"\"This is in quotes\""

Examples of string literals are:

public class Main {
  public static void main(String[] argv) {
    String s = "";
    System.out.println("s is " + s);

    s = "two\nlines";
    System.out.println("s is " + s);

    s = "\"quotes\"";

    System.out.println("s is " + s);


The output generated by this program is shown here:

s is
s is two
s is "quotes"

Java strings must begin and end on the same line.

public class Main {
  public static void main(String[] argv){
     String s = "line 1
                 line 2


If you try to comiple this program, the compiler will generate the following error message.

D:\>javac unclosed string literal
     String s = "line 1
                ^ not a statement
                 line 2
                 ^ ';' expected
                 line 2
                     ^ unclosed string literal
4 errors

In Java strings are actually object types.

Arrays of strings

You can define String array as you define the int array. For example:

public class Main {
  public static void main(String args[]) {
    String str[] = {"one", "two", "three",""};

    for (int i = 0; i < str.length; i++)
      System.out.println("str[" + i + "]: " + str[i]);

Here is the output from this program:

str[0]: one
str[1]: two
str[2]: three
  Java Book 
    Essential Classes  

  1. String type and Literals
  2. String Concatenation
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String