Strings - an example of a class type
Java provides many classes that represent data types, e.g. String. To declare a String variable and create a string: String s = new String (“This is the text.”);
But in the case of strings, the special syntax is allowed: String s = “This is the text.”;
Once created, individual characters of a string cannot be changed in place. The following example uses String methods to create a new string:
- String test = "Chicken soup with rice";
- int n = test.indexOf( 'w' );
- String newtest = test.substring(1,n-1) + "is n" + test.substring(n+5);
- /* giving "Chicken soup is nice" */
Comparing strings - use method equals instead of “==“ test.equals ( newtest )