Java – The String Class

String class in Java

We have previously covered: The Number Class and  The Character Class. In this tutorial, we would examine the String class.

I believe you understand these by now. If not, I rather suggest you take some time to review them. Today we would cover the following:

  1. Introduction to Strings
  2. Creating a String
  3. Length of a String
  4. Combining Strings
  5. Formatting Strings

 

1. Introduction to Strings

Now, we are going to look at the String class.

First, keep in mind that Java does not provide a primitive type for strings. So we are left with only the wrapper class String. It therefore means that all strings in Java are objects. You need to stick with that. You will find it interesting though. I will also want you to know that a string object is also a char array. That is array of char types/

 

2. Creating a String

First you need to learn how to create a string. There are two ways of doing this:

  • first: by assigning a string literal
  • second: by using the string constructor
  • third: by creating a string from a char array

 

I therefore provide the program in Listing 1.0. You can see how strings are created using these three methods. I also recommend you make sure to run it yourself.

 

public class StringTester {
	
	public static void main(String[] args) {
		//first method
		String title = "Java Tutorials";
		
		//second method
		String messgage = new String("Good Job!");
		
		//third method: a char array
		char[] charArray = {'H','e','l','l','o'};
		String greeting = new String(charArray);
	}
}

Listing 1.0: Ways to create a string

 

It is noteworthy that Strings are immutable. You need to keep this in mind. Hence, once a string object is created, then it cannot be changed. To be able to make changes to strings, we use the String Buffer and String Builder classes.

 

3. Length of a String

You can get information about string objects. To do this, you use methods provided by the string classed. These methods are known as accessor methods.

One of such methods, you can use is the length() method. You use this method to get the length of the string. That is how many characters are in the string.

I recommend you run the program in Listing 1.1. In this way, you understand how to use the length() method.

 

public class StringTester {
	
	public static void main(String[] args) {
		//first method
		String title = "Java Tutorials";
		
		int length = title.length();
		
		System.out.print(length);
	}
}

Listing 1.1: Program to get the the length of a string

 

If you run the program in Listing 1.1, then you will get an output of 14. You should also note that the white-space between the two words is also a character.

 

4. Combining Strings (Concatenation)

I therefore suggest you add this word to your list of important words – concatenation. It means combining two or more strings to form one string. So we say something like: to concatenate two strings.

This is achieved by either of two ways:

  • by using the concatenation operator (+)
  • by using the concat() method. I think it’s fairly easy to use.

So to combine two strings, string1 and string2 using the concatenation operator, you say:

 

string3 = string1 + string2

 

But to use the concat method, you  say:

 

string3 = string1.concat(string2)

 

So take a loop at the program in Listing 1.2 below. I suggest you run it yourself in your own IDE.

public class StringTester {
	
	public static void main(String[] args) {
		String string1 = "Java";
		String string2 = "Tutorials";
		
		//using concatenation operator (+)
		String string3 = string1 + string2;
		
		//using the concat method
		String string4 = string1.concat(string2);
		
		System.out.println(string3);
		System.out.println(string4);
	}
}

Listing 1.2: Combining two strings

 

If you run the code in Listing 1.2, it would display the same result. JavaTutorials. So, string3 and string4 would hold the same value

 

5. Formatting Strings

You can you two functions to create formatted strings:

  • the printf() function
  • the format() function

 

While the printf() function is available under System.out namespace, the format() method is a method in the String class. Nevertheless, you can use both to achieve the same result.

 

Take an example in the code below.

public class StringTester {
	
	public static void main(String[] args) {
		String name = "Osondu Mills";
		float score = 87.09f;
		int seat_number = 14;
		
		System.out.printf("My name is %s " +
				"and my score is %f and " +
				"my seat number is %d ",
				name, score, seat_number);		
	}
}

Listing 1.3: Formatting String using printf()

 

If you run the program above in Listing 1.3, the you will have the result below

My name is Osondu Mills and my score is 87.089996 and my seat number is 14 

 

%s, %f and %d are called format specifiers. %s is used to represent strings, %d is used to represent integers while %f is used to represent floats

So what happens here is the each formats specifier is replaced with the value of the corresponding string variable.

 

  • %s is replaced with “Osondu Mills”
  • %f is replaced with 87.09
  • %d is replaced with 14

 

Hence, you have the specified output. Now the code in Listing 1.4 does exactly the same thing. But this time, using the format method of the string class.

You can also try it.

 

public class StringTester {
	
	public static void main(String[] args) {
		String name = "Osondu Mills";
		float score = 87.09f;
		int seat_number = 14;
		
		//Using the format method
		String details = String.format("My name is %s " +
				"and my score is %f and " +
				"my seat number is %d ",
				name, score, seat_number);

		System.out.println(details);	
	}
}

Listing 1.4: Using the format method of the string class