In the previous lesson you learnt the fundamentals of primitive datatypes like char, int, double, float and boolean. We also learnt about Strings, although they are not strictly a primitive datatype. In that lesson you learnt about bits, nibbles and bytes, and how much memory space datatypes occupy. You also learnt how to declare and initialise variables using each of these datatypes.
What are Strings in Java?
Strings are one of the most flexible datatypes and are widely used in Java programming. Typically you will find them in the sentences, the paragraphs, and error messages that are part and parcel of any working program. They are essentially a sequence of characters that are created in the same way as primitive datatypes but they are actually objects. Later, when we talk about arrays, you will see that an array of characters is identical to a Java string. Strings can contain any character type (letters, numbers, spaces, punctuation, etc.) which makes them one of the most versatile datatypes available to programmers. All characters stored in a string are of type String and will need to be converted to a suitable datatype if necessary, for example a number stored as a string will need to be converted to a numeric datatype before it can be used as a number.
Strings are created by enclosing zero or more characters (any characters, numbers and/or symbols) enclosed in double quotation marks, the general syntax is:
String variableName = “String Content”;
This type of string definition is called a String Literal. It is an efficient and the most common way of creating strings. As mentioned earlier, a string is an object that represents a sequence of characters, and it uses the java.lang.String class to create a string object. More about Classes and Objects is later lessons but for now, know that an object is an instance of a class. Each time a string literal is created, a special memory area called the string constant pool is checked to see if the string already exists in the pool. If it does, then a reference to the pooled object (or instance) is returned. However, if the string doesn’t exist in the pool, a new string instance is created and placed in the pool.
There is another way to create a string, here is the general syntax:
String variableName =new String(“String Content”);
This type of string definition uses the new keyword. It will always create two objects and one reference variable. The first string object is placed in the normal (non-pool) heap memory and the string literal is placed in the string constant pool. The variable (in this example variableName) will refer to the object in a heap (non-pool).
Strings are regarded as an array of characters and so use a number of array characteristics – arrays are covered in a later lesson. This means we can apply certain predefined methods to them, methods that return the number of characters in a string or what the string string start or ends with. One thing to bear in mind is that Java is a case sensitive language, and this includes strings.
Throughout this lesson you are going to learn how to manage, manipulate and modify Strings for your programs by:
- Understanding and finding the length of a String using the length() method
- Joining two or more Strings together
- Understanding what substrings are and how to use them
- Comparing Strings using comparative string methods
- Removing unwanted space from Strings using the trim() Method
- Changing characters within a String using the replace() Method
- Changing case of a String
What you will learn
In this lesson you will learn about Strings as they are used in Java. You will be introduced to a variety of methods that can be used to access, modify, join, split and compare strings.