"Hi, Amigo!"
"But, Bilaabo, you've already said hello."
"Really? Well, I still like starting each lesson with that phrase."
"Today we're going to study the String class in depth."
"But, I already know everything about it. I even know that the String class is immutable."
"The String class has 46 methods. How many of them do you know?"
"No more than ten. Actually, probably about 5 max."
"Then listen up."
"Java's creators noticed that most strings in programs aren't intended to be modified, but sometimes they get modified anyway. It's very annoying when you create a String, save something important in it, pass it to someone else's method, and it gets changed. To keep this from happening, they decided to create both mutable and immutable Strings."
"The String class is for immutable strings, and the StringBuilder class is for mutable ones. Objects of these classes are easily converted to the other type. In most cases, Java developers simply need String, which proves that Java's creators were right."
"So if I need a String, I use the String class. And if I need a mutable String, then I use the StringBuilder class?"
"Yes. Actually, there is another class called StringBuffer. It's a copy of StringBuilder, except all of its methods are declared as synchronized, so that the object can be accessed from different threads of the program."
"And what about this example? Doesn't the string get changed here?"
String s = "cat";
s = s + "-" + s;
"No. There are two String objects here: «cat» and «cat-cat». The second is created using the first, but the first object doesn't change. In reality, things are far more interesting here. Here's the code the compiler generates when it compiles your example:"
String s = "cat";
StrinsBuilder s2 = new StringBuilder(s);
s2.append("-");
s2.append(s);
s = s2.toString();
"So, StringBuilder is almost always used to create new Strings, but sometimes the compiler simply does all of the work for you. But your version is simpler, don't you think?"
"Yeah, it's awesome that Java has such an advanced compiler."
"Well, now let's walk through the String class's methods:"
1) How do I find a String's length?
"The length method returns a String's length (the number of characters in it)."
Method(s) | Example(s) |
---|---|
|
|
|
2) How do I get a character from a String?
"The charAt method returns a character from a String by its index. The character indices start at 0.
Method(s) | Example(s) |
---|---|
|
|
|
3) How do I get characters from a String?
How do I get characters from a String?
Method(s) | Example(s) |
---|---|
|
|
4) How do I compare Strings?
"The equals method checks if the Strings match, and the equalsIgnoreCase method checks to see if the Strings match when case is ignored.
Method(s) | Method(s) |
---|---|
|
|
|
|
5) How do I make all of the letters in a String uppercase or lowercase?
"The toUpperCase method returns a copy of the String with all uppercase letters."
"The toLowerCase method returns a copy of the String with all lowercase letters."
Method(s) | Example(s) |
---|---|
|
|
Result:
|
|
|
|
Result:
|
6) How do I remove spaces at the beginning and end of a String?
"The trim method returns a copy of a String with no whitespace characters at the beginning and end."
Method(s) | Example(s) |
---|---|
|
|
Result:
|
GO TO FULL VERSION