Docsity

Java's String Class: Understanding Immutable Strings and Concatenation, Assignments of Computer Science

Toggle sidebar
bg1
Java's String Class
So far in this course, we've seen what a class declaration looks like, and we've seen
how to create objects and perform operations on them. Much of the time, however,
we won't need to create our own classes, because the Java API (Application
Programming Interface, also called the Java class library) provides a huge number of
prewritten classes, ready for us to use. Of all the classes in the Java API, the String
class is probably the most important. Instances of the String class represent strings
of characters, which are used in virtually every Java program
The String class belongs to a package named java.lang. This package is
automatically imported into every program, so we don't need an import declaration
in order to use String. By the way, java.lang is the only package that is
automatically imported. All others must be specifically imported by using the import
statement above your class declaration.
Creating Strings
In Java, every string of characters, such as "abc", is an instance of the String class.
We can declare String variables, which can then be assigned String objects as their
values:
String greeting;
the String variable greeting is capable of storing a String object.
String objects can be created without using the keyword new. String is the only
class whose instances can be created without the keyword new (the Java designers
decided to treat the String class differently because of its importance and frequent
use).
The following examples both create a String object, but Example 2 is the more
common way:
Example 1
String greeting = new String("Howdy!");
Example 2
String greeting = "Howdy!";
We can visualize a String object as a series of characters, with each character
identified by its position. The first character is located at position 0. The string
"Howdy!", for example, has the letter H at position 0, o at position 1, and w at position
2.
Immutability of Strings
pf3

Partial preview of the text

Download Java's String Class: Understanding Immutable Strings and Concatenation and more Assignments Computer Science in PDF only on Docsity!

Java's String Class

So far in this course, we've seen what a class declaration looks like, and we've seen how to create objects and perform operations on them. Much of the time, however, we won't need to create our own classes, because the Java API (Application Programming Interface, also called the Java class library ) provides a huge number of prewritten classes, ready for us to use. Of all the classes in the Java API, the String class is probably the most important. Instances of the String class represent strings of characters, which are used in virtually every Java program The String class belongs to a package named java.lang. This package is automatically imported into every program, so we don't need an import declaration in order to use String. By the way, java.lang is the only package that is automatically imported. All others must be specifically imported by using the import statement above your class declaration. Creating Strings In Java, every string of characters, such as "abc", is an instance of the String class. We can declare String variables, which can then be assigned String objects as their values: String greeting; the String variable greeting is capable of storing a String object. String objects can be created without using the keyword new. String is the only class whose instances can be created without the keyword new (the Java designers decided to treat the String class differently because of its importance and frequent use). The following examples both create a String object, but Example 2 is the more common way: Example 1 String greeting = new String("Howdy!"); Example 2 String greeting = "Howdy!"; We can visualize a String object as a series of characters, with each character identified by its position. The first character is located at position 0. The string "Howdy!", for example, has the letter H at position 0, o at position 1, and w at position

Immutability of Strings

Objects of the String class are designed to be immutable, that is they are read-only. If you examine the online documentation for the String class, you’ll see that every method in the class that appears to modify a String really creates and returns a brand new String object containing the modification. The original String is left untouched. The two examples above are equivalent. They both create a new String object and give it an initial value - "Howdy!" in this case. Any attempt to modify this String will actually create a new String object - the original String stays the same. For example, the assignment statement below creates a new String object with the value "Hello" and the original String object remains untouched. greeting = "Hello"; Since there are no references to the original String object (greeting has been reassigned to reference the new String object), the Java garbage collector will automatically free up this memory. Since String objects are immutable, you can alias to a particular String as many times as you want. Because it’s read-only there’s no possibility that one reference will change something that will affect the other references. Using + to Concatenate Strings One of the most common string operations is concatenation : joining two or more strings together to form a single string. The String class provides a concat method that performs concatenation, but it's rarely used. Concatenation of strings is so common that Java provides a shortcut. To concatenate strings, we simply put a plus sign (+) between them, as though we were adding two numbers. When used with String objects, the ‘ + ’ allows you to concatenate String s together: String newGreeting = greeting + "Y'all!"; But that's not all! It turns out that the + operator works even if one of the operands isn't a String object. The non-String operand is converted to string form automatically before concatenation is done. We first noticed this property when we were able to write System.out.println("Celcius equivalent: " + celcius); The + operator converts the value of celcius into a string, and then concatenates it to the end of "Celcius equivalent: ". Common String Methods Here is an overview of the methods available for the String class. This table doesn’t contain every single method, but rather the ones that are used most commonly. Method Description