• Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.


  • The Java platform provides the String class to create and manipulate strings.


  • Creating Strings The most direct way to create a string is to write:


  •  
    String greeting = "Hello world!";
    
    
  • In this case, "Hello world!" is a string literal—a series of characters in your code that is 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!.


  • As with any other object, you can create String objects by using the new keyword and a constructor.


  • The String class has thirteen constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:


  •  
    char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
    String helloString = new String(helloArray);
    System.out.println(helloString);
    
    
    
    The last line of this code snippet displays hello.
    
    
  • Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.


  • String Length Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object. After the following two lines of code have been executed, len equals 17:


  •  
    String palindrome = "Dot saw I was Tod";
    int len = palindrome.length();
    
    
    
  • A palindrome is a word or sentence that is symmetric—it is spelled the same forward and backward, ignoring case and punctuation.


  • Here is a short and inefficient program to reverse a palindrome string. It invokes the String method charAt(i), which returns the ith character in the string, counting from 0.


  •  
    
    public class StringDemo {
        public static void main(String[] args) {
            String palindrome = "Dot saw I was Tod";
            int len = palindrome.length();
            char[] tempCharArray = new char[len];
            char[] charArray = new char[len];
            
            // put original string in an 
            // array of chars
            for (int i = 0; i < len; i++) {
                tempCharArray[i] = 
                    palindrome.charAt(i);
            } 
            
            // reverse array of chars
            for (int j = 0; j < len; j++) {
                charArray[j] =
                    tempCharArray[len - 1 - j];
            }
            
            String reversePalindrome =
                new String(charArray);
            System.out.println(reversePalindrome);
        }
    }
    
    
    
    Running the program produces this output:
    
    doT saw I was toD
    
    
    
  • To accomplish the string reversal, the program had to convert the string to an array of characters (first for loop), reverse the array into a second array (second for loop), and then convert back to a string.


  • The String class includes a method, getChars(), to convert a string, or a portion of a string, into an array of characters so we could replace the first for loop in the program above with


  •  
    palindrome.getChars(0, len, tempCharArray, 0);
    
    
    
  • Concatenating Strings The String class includes a method for concatenating two strings:


  •  
    string1.concat(string2); 
    
    
    
  • This returns a new string that is string1 with string2 added to it at the end.


  • You can also use the concat() method with string literals, as in:


  •  
    "My name is ".concat("Rumplestiltskin");
    
    
    
  • Strings are more commonly concatenated with the + operator, as in


  •  
    "Hello," + " world" + "!"
    
    
    which results in
    
    
    "Hello, world!"
    
    
    
  • The + operator is widely used in print statements. For example:


  •  
    String string1 = "saw I was ";
    System.out.println("Dot " + string1 + "Tod");
    which prints
    
    Dot saw I was Tod
    
    
    
  • Such a concatenation can be a mixture of any objects. For each object that is not a String, its toString() method is called to convert it to a String.


  • Note: The Java programming language does not permit literal strings to span lines in source files, so you must use the + concatenation operator at the end of each line in a multi-line string. For example:


  •  
    String quote = 
        "Now is the time for all good " +
        "men to come to the aid of their country.";
        
        
        
    
  • Breaking strings between lines using the + concatenation operator is, once again, very common in print statements.


  • Creating Format Strings You have seen the use of the printf() and format() methods to print output with formatted numbers.


  • The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object.


  • Using String's static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of


  •  
    System.out.printf("The value of the float " +
                      "variable is %f, while " +
                      "the value of the " + 
                      "integer variable is %d, " +
                      "and the string is %s", 
                      floatVar, intVar, stringVar); 
    
    
    you can write
    
    
     
    String fs;
    fs = String.format("The value of the float " +
                       "variable is %f, while " +
                       "the value of the " + 
                       "integer variable is %d, " +
                       " and the string is %s",
                       floatVar, intVar, stringVar);
    System.out.println(fs);
    
    
    
  • Frequently, a program ends up with numeric data in a string object—a value entered by the user, for example.


  • The Number subclasses that wrap primitive numeric types ( Byte, Integer, Double, Float, Long, and Short) each provide a class method named valueOf that converts a string to an object of that type.


  • Here is an example, ValueOfDemo , that gets two strings from the command line, converts them to numbers, and performs arithmetic operations on the values:


  •  
    public class ValueOfDemo {
        public static void main(String[] args) {
    
            // this program requires two 
            // arguments on the command line 
            if (args.length == 2) {
                // convert strings to numbers
                float a = (Float.valueOf(args[0])).floatValue(); 
                float b = (Float.valueOf(args[1])).floatValue();
    
                // do some arithmetic
                System.out.println("a + b = " +
                                   (a + b));
                System.out.println("a - b = " +
                                   (a - b));
                System.out.println("a * b = " +
                                   (a * b));
                System.out.println("a / b = " +
                                   (a / b));
                System.out.println("a % b = " +
                                   (a % b));
            } else {
                System.out.println("This program " +
                    "requires two command-line arguments.");
            }
        }
    }
    The following is the output from the program when you use 4.5 and 87.2 for the command-line arguments:
    
    a + b = 91.7
    a - b = -82.7
    a * b = 392.4
    a / b = 0.0516055
    a % b = 4.5 
    
  • Note: Each of the Number subclasses that wrap primitive numeric types also provides a parseXXXX() method (for example, parseFloat()) that can be used to convert strings to primitive numbers.


  • Since a primitive type is returned instead of an object, the parseFloat() method is more direct than the valueOf() method.


  • For example, in the ValueOfDemo program, we could use:


  •  
        
        
        
    public class ValueOfDemo {
        public static void main(String[] args) {
    
        String a ="2";
        String b ="3";
        
        
        double a1 = Double.parseDouble(a);
        double b1 = Double.parseDouble(b);
    
         System.out.println("a + b = " +
                                   (a1 + b1));
                System.out.println("a - b = " +
                                   (a1 - b1));
                System.out.println("a * b = " +
                                   (a1 * b1));
                System.out.println("a / b = " +
                                   (a1 / b1));
                System.out.println("a % b = " +
                                   (a1 % b1));
          
            
            
            
        }
    }
    
    Output:
    
    
    a + b = 5.0                                                                                        
    a - b = -1.0                                                                                       
    a * b = 6.0                                                                                        
    a / b = 0.6666666666666666                                                                         
    a % b = 2.0
    
    
  • Converting Numbers to Strings Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string:


  •  
    int i;
    // Concatenate "i" with an empty string; conversion is handled for you.
    String s1 = "" + i;
    or
    
    // The valueOf class method.
    String s2 = String.valueOf(i); 
    
  • Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a string. For example:


  •  
    int i;
    double d;
    String s3 = Integer.toString(i); 
    String s4 = Double.toString(d);  
    
  • The ToStringDemo example uses the toString method to convert a number to a string. The program then uses some string methods to compute the number of digits before and after the decimal point:


  •  
    public class ToStringDemo {
        
        public static void main(String[] args) {
            double d = 858.48;
            String s = Double.toString(d);
            
            int dot = s.indexOf('.');
            
            System.out.println(dot + " digits " +
                "before decimal point.");
            System.out.println( (s.length() - dot - 1) +
                " digits after decimal point.");
        }
    }
    The output of this program is:
    
    3 digits before decimal point.
    2 digits after decimal point.