In this video we're going to start looking at strings.
Strings represent the text we use in applications.
We'll start with the basics in this tutorial, and over the next few videos we'll cover more
complex string topics.
Strings are a collection of characters strung together into a single datatype.
That's why they are called strings.
We can think of them as words and sentences.
They're important because humans don't think directly in numbers.
We need characters and words for our programs.
In Java, strings are represented using the String class.
Strings can be declared similar to numbers using literals.
That looks like this.
We simply declare our string variable, and give it a literal value.
We can also declare a string using a constructor.
We'll rarely ever see this method of creating strings.
The reason is the literal is a class instance.
So this code is really creating a string instance using the literal and passing the instance
to the constructor to create another string.
It's double work creating two strings.
Doing this in a loop a billion times could create big problems for us, since we'd be
creating twice as many string instances as necessary.
That would also create headaches for instance garbage collection.
The String class let's us use and manipulate strings.
For example, we can declare a string like this.
Then we can call a method on the String class.
In this case, we're getting the length of the string.
String literals are also string instances.
So we can do some interesting things syntactically, like call a method on a literal.
Either way works, and it really depends on the specific case as to which form makes more
sense.
Usually we wouldn't call length on a string literal, since we should know the length at
that time.
A better example is testing a string for equality.
We might want to check is a string is equal to a known literal like this.
However, if the string is null, we'll get a null pointer exception.
A better check is to use the method on a literal like this.
The literal will never be null, and if the value is null the equals method will just
return false.
Another thing we need to know about strings is Java tries to help us conserve memory by
doing something called string interning.
What this means is Java recognizes when we do something like this.
We create a string a and a string b, both with the same literal value.
If we test for equality using the equality operator, the test is true.
But we need to be careful here.
This doesn't mean the two strings have the same value.
We use the equals method to test that.
The equality operator means a and b reference the same string instance.
The reason is Java creates a special pool of strings, and reuses the instances when
it can.
This is called string interning.
If we deliberately create a string using the constructor, we'll get different results.
The reason is the string created using the constructor does not use the interned instance.
It creates another instance.
So in this case, a and c, and a and d point to different instances.
Strings are also immutable.
This means once we create an instance, that's the value.
We can't change it.
Changing the value of a string variable like this, creates a new instance.
There are several reasons strings are immutable.
One reason we just saw is interning in the string pool.
If we could change the value, we'd be changing the instance for every place the string was
used, which would defeat our ability to cache strings.
Another reason is security.
Imagine we have a method that takes a string representing a query to a database.
If we could change the string, we could wreak havoc in our database.
Or secretly connect to another database.
It would be bad.
There are other reasons to make strings immutable such as thread safety, and use in other classes
like the Sets and Hashtables.
We'll cover these more later.
So that's the quick look at Java strings.
We saw how to create Java strings, how to test if two strings are equal, and we noted
that strings are immutable.
If you have any questions, add them to the comments below.
We're going to look more at strings in the next few videos, since they are very important
for most Java applications.
If you enjoyed the video, please share and like.
And with that, I’ll see you in the next tutorial!