Today we’ll do a simple exploration of Strings.

The String class is the most commonly used class in the Java library, most obviously because strings are an important part of a programming language.

The most important thing about strings is that every String you create is actually an object of type String, and even String constants are really strings.

System.out.println("This is a String, too"); 
Copy the code

The String “This is a String, too” is a String constant. Fortunately, Java handles string constants the same way other computer languages handle “normal” strings, so you don’t have to worry about this.

Another thing about strings is that objects of type String are immutable; Once a string object is created, its contents cannot be changed. This may seem like a strict limit, but it’s not, for two reasons:

· If you need to change a string, you can create a new string that contains the modified string.

· Java defines an equivalent of the String class called StringBuffer, which allows String changes so that all normal String operations are still available in Java.

Strings can be constructed in a number of ways. The easiest way to do this is with:

String myString = "this is a test"; 
Copy the code

Once you have created a string object, you can use it wherever strings are allowed, such as this statement showing myString:

System.out.println(myString); 
Copy the code

Java defines a String operator: “+”. It is used to concatenate two strings. For example, the following statement:

String myString = "I" + " like " + "Java."; 
Copy the code

MyString contains “I like Java.”

The following program illustrates the previous concept:

// Demonstrating Strings. 
class StringDemo { 
 public static void main(String args[]) { 
  String strOb1 = "First String"; 
  String strOb2 = "Second String"; 
  String strOb3 = strOb1 + " and "+ strOb2; System.out.println(strOb1); System.out.println(strOb2); System.out.println(strOb3); }}Copy the code

The program produces the following output:

First String 
Second String 
First String and Second String 
Copy the code

The String class contains many methods for manipulating strings. For example, here are some of them. You can use equals() to check whether two strings are equal. You can call the length() method to get the length of a string.

You can call charAt() to get a character for a string specified index. The general format of the three methods is as follows:

boolean equals(String object) 
int length( ) 
char charAt(int index) 
Copy the code

The following program examples these methods:

// Demonstrating some String methods. 
class StringDemo2 { 
 public static void main(String args[]) { 
  String strOb1 = "First String"; 
  String strOb2 = "Second String"; 
  String strOb3 = strOb1; 
  System.out.println("Length of strOb1: " + 
  strOb1.length()); 
  System.out.println("Char at index 3 in strOb1: " + 
  strOb1.charAt(3)); 
if(strOb1.equals(strOb2)) 
 System.out.println("strOb1 == strOb2"); 
else 
 System.out.println("strOb1 ! = strOb2"); 
if(strOb1.equals(strOb3)) 
 System.out.println("strOb1 == strOb3"); 
else 
 System.out.println("strOb1 ! = strOb3"); }}Copy the code

The program produces the following output:

Length of strOb1: 12 
Char at index 3in strOb1: s strOb1 ! = strOb2 strOb1 == strOb3Copy the code

Of course, strings can be an array just like any other object type, for example:

// Demonstrate String arrays. 
class StringDemo3 { 
 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]); }}Copy the code

Here is the output produced by the program:

str[0]: one 
str[1]: two 
str[2]: three 
Copy the code

As you will see in the next tutorial, string arrays play an important role in many Java programs.