Practice Exams:

1z0-808 Oracle Java SE 8 Programmer – Strings

  1. Section Overview

In this section, we’re going to focus on strings. And you’ve been working with strings, so you know that that’s just text. But strings are very unique in the Java programming language. They’re objects. But we can do things to strings that we normally can only do with primitives. We’re also going to COVID the subject of immutability in this section.

  1. String Objects

I’m willing to bet that as you code more and more in Java and you take a look at your applications, that the number one or very, very close to number one object that you’ll use over and over again is going to be the string object. So we’re going to take a little bit of time to talk about strings and how they behave in Java. They are special in Java and they’re special for a couple of reasons. Reasons. Now, first and foremost, a string is an object and so it behaves just like an object. There is state which would be the text and then there’s a lot of behavior. There’s tons of methods that you can use on a string. And as a side note, for those of you taking the exam, we’re not going to COVID all the string methods that are available here, but it is your job to go and read the string API and learn the methods that they have. I’ll talk about some high level things to remember about strings, but you’re going to want to become familiar with most of the methods that are in the API. So strings behave like objects, but we can do things with strings in Java that you typically can only do with primitives. And we’ll show you some of that syntax coming up in this section.

Now, let’s start by looking at the string object. So here’s some code where we say string S equals new string and we’re creating a string object for Hello World. So S right now is on the stack and it is referring to the Hello, World object on the heap. And you can see that the heap are a set of characters, a set of chars. Now, these chars are sequenced and they are sweet sequence, starting with the index of zero. So here’s what Hello, World really looks like. Index zero has a letter H, index six has a letter W, index ten has a letter D and so on. And the numbers that we’re seeing here become important because whenever you’re using methods on the string object, sometimes they want to know ranges. Like if I wanted to get a substring, I want to get just the word hello out of there. So what I could do is pass in the numbers zero comma five and it would go from zero up till five and would then have then would return a string that just contains hello.

So keep that in mind that a string is a sequence of characters and those characters are indexed starting with zero. So getting back to how strings are instantiated, it’s a normal object. It’s a class that’s written and part of the Java API. It’s in the system, the Java Lang package. And here again, another way or the same way to create the string that we just saw. String S one equals new string Mulligan. However, because we use strings so often in Java that they’ve given us a literal syntax to use as well. And so the literal syntax says you can drop the whole new string if you’d like, just put quotes around the text that you want and that will become a string object. So double quotes is a string literal, just like single quotes is a character literal, or a whole number is an integer literal and a number with

 

a decimal is a double literal. So now we’ve got string s two equals Seamus. Now there is one key difference between those two strings and it’s really how Java treats the objects on the heap. What happens is when you create a string object using the new keyword, like we did with Mulligan, then what happens is the string object will be created even if another string exists with the same name. And that would be true even if all the characters are the same of another string. What do I mean by that? Well, let’s look at some code. So here we’ve got two strings that we’re creating and they both have the exact same sequence of characters.

We’ve got mulligan and mulligan. And so as you say, new string mulligan. Every time you say it, it creates a new object on the heap. Now you’re probably thinking, well, well, duh. I mean, of course it does. That’s what I would expect. You made two objects, why wouldn’t you have two strings? Well, because strings are so predominant in application that it’s kind of a waste of space to keep having the same string on the heap over and over again. So when you use the string literal syntax like we’re doing here, string, my string three is Seamus and mystring four is also Seamus. Then what it does is it looks in a string pool and it says, does this string already exist? Is there a sequence of characters that matches the literal that you’ve just tried to create? And if so, then both of these string variables on the stack will be pointing to the exact same string on the heap.

Now of course the benefit is that we’re saving some memory, but there’s some problems there too, right? Imagine that mystring three is in one section of code and mystring four is in some section of code. What would happen if my string three changed the string? Would mystring four see that change? And that’s a question we’re going to answer in a little bit. It’s also legal to create a string with no characters. So here we’ve got string S equals quote, quote. And we refer to that as an empty string. We want to be clear that is not the same thing as null. That’s different than saying string S equals null. Null and empty string are totally different. An empty string is still a string object. All of the methods that are available to string are available on that empty string. Null means that the reference isn’t referring to anything. So there is no object to back it up. Now, before we talk about the whole what happens if a reference variable tries to change a string? What happens to the other variables that are pointing to that string? We’ll talk about what happens there in just a little bit. But before we do, let’s talk about string operations and operators in the next lecture.

  1. String Operations and Operators

We’ve been looking at some of the different methods for a string. In many of the previous lectures we saw substring at one point, we saw trim at another. And what we’re going to do is kind of revisit those methods and just take a look at how the string can be manipulated. Now, so we’ll start by creating a string. We’re doing a new string saying Hello World. Notice that there’s some spaces after the word world. So we’ve got a little bit of white space in the string itself. Then we’re calling the method substring. So we’re using that dot operator saying s dot substring and we’re passing in two integers. Now, how would I know that it requires two integers?

Well, I’d have to go to the API documentation. From there I would discover that there is a method called substring and we’d see that it takes two parameters. The first parameter is the index that we should start at it’s inclusive. It’ll include that character. In other words, think about a string. Hello world. We’ve got zero for the H 1234. So we’re going to start with the letter O and then it says it’s going to continue on until the 10th character. But that’s exclusive. So that will get us all the way to L because D is the 10th indexed character.

So that gives us a Whirl back. Now, here’s something that’s very interesting about strings and something you have to remember especially for the exam that a string is immutable. Now, I’m going to talk a lot more about that in a second, but I want to bring it up now so that you know that when we call substring that it’s not actually changing the original string. What it’s going to do is it’s going to create a whole brand new string and then it’s going to return it.

So if I didn’t capture that return value like I’m doing here with string sub, if I didn’t capture that new orl string, then we’d lose it and it would eventually be garbage collected. So that’s A gotcha on the exams. Make sure that whenever you’re working with strings and you call these methods that in fact it does return a string and that that string is captured. Otherwise it kind of looks like you’re changing the string and we’re not really doing that. We’re creating a new string. So here’s another one s trim. And so what that’s going to do? Trim will remove the white space before and after. At the end, it doesn’t do anything in the middle. So in that case, it’s going to return a brand new string called Hello World with no white space. And we also have two uppercase that would return hello world.

Notice the spaces are back because when we called on S, s never changed that Hello World string still exists and so it still has the spaces even though we called Trim because Trim isn’t removing the spaces of the existing string it’s creating a brand new string without the spaces. So now we’ve got to uppercase and that’s going to return. Hello world. Here’s a method called char at character at. And so here we’re passing the index of two. So remember that’s zero one, two. So that will return an L and we take that return value and store it with a character. And so there are lots and lots of methods that are available to you. And that’s because a string is an object. It’s an object that has lots of methods. So how do you find out what methods there are? Like I said, go to the API documentation for Java lying string. Here’s a little snippet. You’ll see all the different methods and what they return.

 Just remember that whenever you are calling a method here like concat or substring, and it returns a string, that it is in fact a brand new string. So that’s Java behaving like an object as we would expect, it has methods, it has behavior. But I mentioned that we can do things with strings that we typically can only do with a primitive. And one of those things is that we can use the plus operator on a string. And what that does is it appends or concatenates strings to one another. So here’s an example. I’ve got a string here called Greet.

It says Hello, I make another string called Name set to Dave. And now I have a third string where I’m concatenating greet plus name. All right? So that string would be hello, Dave. And that’s something you can’t do with any other object. I couldn’t use the plus operator on anything other than a primitive or on a string. So again, the string now points to a literal called Hello Dave. We can also use assignment operators with string objects. So here’s another example.

We’ve got a string STR for my dog and we can now say STR plus equals. If you remember, the plus equals assignment operator means that we’re taking the string and we’re going to concatenate has fleas to it, and then assign that new value back to the variable STR. So now if we print out the STR variable, it will say my dog has fleas.

  1. Immutable Strings

We’ve been looking at some of the different methods for a string. In many of the previous lectures we saw substring at one point, we saw trim at another. And what we’re going to do is kind of revisit those methods and just take a look at how the string can be manipulated. Now, so we’ll start by creating a string. We’re doing a new string saying Hello World. Notice that there’s some spaces after the word world. So we’ve got a little bit of white space in the string itself. Then we’re calling the method substring. So we’re using that dot operator saying s dot substring and we’re passing in two integers. Now, how would I know that it requires two integers?

Well, I’d have to go to the API documentation. From there I would discover that there is a method called substring and we’d see that it takes two parameters. The first parameter is the index that we should start at it’s inclusive. It’ll include that character. In other words, think about a string. Hello world. We’ve got zero for the H 1234. So we’re going to start with the letter O and then it says it’s going to continue on until the 10th character. But that’s exclusive. So that will get us all the way to L because D is the 10th indexed character.

So that gives us a Whirl back. Now, here’s something that’s very interesting about strings and something you have to remember especially for the exam that a string is immutable. Now, I’m going to talk a lot more about that in a second, but I want to bring it up now so that you know that when we call substring that it’s not actually changing the original string. What it’s going to do is it’s going to create a whole brand new string and then it’s going to return it. So if I didn’t capture that return value like I’m doing here with string sub, if I didn’t capture that new orl string, then we’d lose it and it would eventually be garbage collected.

So that’s A gotcha on the exams. Make sure that whenever you’re working with strings and you call these methods that in fact it does return a string and that that string is captured. Otherwise it kind of looks like you’re changing the string and we’re not really doing that. We’re creating a new string. So here’s another one s trim. And so what that’s going to do? Trim will remove the white space before and after. At the end, it doesn’t do anything in the middle. So in that case, it’s going to return a brand new string called Hello World with no white space. And we also have two uppercase that would return hello world. Notice the spaces are back because when we called on S, s never changed that Hello World string still exists and so it still has the spaces even though we called Trim because Trim isn’t removing the spaces of the existing string it’s creating a brand new string without the spaces. So now we’ve got to uppercase and that’s going to return.

Hello world. Here’s a method called char at character at. And so here we’re passing the index of two. So remember that’s zero one, two. So that will return an L and we take that return value and store it with a character. And so there are lots and lots of methods that are available to you. And that’s because a string is an object. It’s an object that has lots of methods. So how do you find out what methods there are? Like I said, go to the API documentation for Java lying string. Here’s a little snippet. You’ll see all the different methods and what they return. Just remember that whenever you are calling a method here like concat or substring, and it returns a string, that it is in fact a brand new string. So that’s Java behaving like an object as we would expect, it has methods, it has behavior. But I mentioned that we can do things with strings that we typically can only do with a primitive. And one of those things is that we can use the plus operator on a string. And what that does is it appends or concatenates strings to one another. So here’s an example. I’ve got a string here called Greet. It says Hello, I make another string called Name set to Dave. And now I have a third string where I’m concatenating greet plus name. All right? So that string would be hello, Dave.

And that’s something you can’t do with any other object. I couldn’t use the plus operator on anything other than a primitive or on a string. So again, the string now points to a literal called Hello Dave. We can also use assignment operators with string objects. So here’s another example. We’ve got a string STR for my dog and we can now say STR plus equals. If you remember, the plus equals assignment operator means that we’re taking the string and we’re going to concatenate has fleas to it, and then assign that new value back to the variable STR. So now if we print out the STR variable, it will say my dog has fleas.

  1. Pass By Value: Primitives

All right. Now let’s look and see what happens when we pass in an object reference to a method. So we’ve got a method called debit fee and we’re going to pass in the account reference. So when debit fee is called, a new frame will be created. This is the method implication. We’re going to say debit fee passing in the MyAccount variable and here’s the definition. So what’s going to happen is this debit fee is going to accept in an account and when debit fee is added to the stack, gets its own frame, you can see that there’s only one variable that’s going to be added to that frame and that’s the account. So ace this account right where’s my cursor? The account right here. So that’s what’s going to be added to the frame and then inside of the method.

What are we doing? Well, we’re using the dot operator to send that object a message. We’re picking up that telephone and saying, hey account, can you tell me what your balance is? Can you read me your balance? Now that’s a read because it’s on the right side of the equal sign. So it does a read, it says, hey, it’s 100. And we do a little calculation in here. We subtract 250. Then we’re going to take that new total and call account again and say, I want you to set your balance. I want to do a write. Set your balance to be this new 197 50. So how does that manifest itself? What happens then when we go back into our main method and say my account balance, what will we see? Well, here’s what the stack and the heap look like.

So debit fee gets a frame on the stack and the main method is going to copy the reference. So remember, what is the value of the my account? It’s this arrow that’s the value. The object is not the value, it’s the reference to the value. So that arrow, that reference is getting copied into the debit fee frame, into the account ACCT variable. So they each have their own reference but they are referring to the exact same account.

So now when I do something to it, when I do a write on the account balance, that write is actually going to happen here on the heap rather than just in the debit fee frame. So because we were sending a message to the account object on the heap, rather than just doing some kind of a calculation inside of the debit fee method itself, that we really changed the balance, we changed the balance of the object on the heap to 97 50. So once debit fee is done, that frame has popped off the stack, the account balance is still 97 50. So now when we say system out, print line MyAccount balance, we’re still picking up the phone in my account, sending a message to the account object on the heap and saying, hey, what is your balance? And so in the main method, we do see the new balance. So for the final example, we’re going to look and see what happens with the string. So the method we’re going to call a salutation and we’re going to pass in that customer name, which is referring to the string Jason Shapiro into the method. Now, the method definition accepts a string and once it executes, it’s going to concatenate the word deer in front of the name and then reset a name to this new value. So a name equals deer a name. So when we first call salutation, this is what the stack and the heap look like. Once again, customer name is going to be passed in by value.

And the value is the reference. String is an object, right? So this is the reference to the object that’s going to be copied and passed into the salutation method. A name gets its own space in the frame salutation, which is now referring to the exact same string. They’re both referring to them just like was happening with account in the previous example. But what happens now when we try to execute the code dear a name.

The problem with or not the problem, the reality of strings is that they’re immutable. And so as soon as we make a change, it’s not going to change the original string, it’s going to create a brand new one. So customer name is still referring to Jason Shapiro, whereas a name is now referring to a brand new string called Deer, Jason Shapiro. So once Salutation is finished executing, it’s going to get popped off the stack. That whole unnamed variable is gone. It’s fallen out of scope. And so that string dear Jason Shapiro, nothing’s referring to it anymore. And so eventually it’ll be garbage collected.

  1. Passing By Value: Objects

In the previous lecture, we talked about passing parameters and we saw that we’re always passing by value. And so now you’re going to have a chance to see that in action yourself. You’re going to code methods where you’re passing in a primitive, where you’re passing in an object and a string. And if you do the bonus lab, which is right at the very end of the lab instructions,

you’ll also be working with a string builder. So go to the resource section for this lecture. Download lab seven parameter passing. It’s a PDF with all the instructions for this lab. If you’ve run into any trouble, there is solution code written at the very back of the lab. And of course, you can always ask me questions as well.