image
String Methods Part 1

Contents

Strings and the StingBuilder Class
1
Course Overview
PREVIEW1m 4s
2
String Methods Part 1
PREVIEW12m 55s
3

The course is part of this learning path

String Methods Part 1
Difficulty
Intermediate
Duration
47m
Students
141
Ratings
5/5
starstarstarstarstar
Description

This course delves into the String class and looks at the functionality it provides through a host of methods. We will also discuss how string objects are immutable, which means the object itself can't be modified once they've been created.

Learning Objectives

  • Learn how the StringBuilder class differs from the string class

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In previous sections, we've used the string class which can represent character that is textual data. The string class is a reference type, but its component parts are characters, which are, of course, primitives. In this lecture, we'll take a look at some of the other useful methods that the string class offers. We will focus on the charAt, length, equals, and compareTo methods. We can view strings as arrays of characters where the first character in the string has index zero, the second character has index one, and so on. You can, in fact, obtain the character at a particular index using the charAt method. You can even use the length method of the string class to determine how many characters are in the string?

The equals and compare two methods are used for comparing strings. So, about comparing strings, how exactly does that work? Underlying each character in java is an integer value representing the Unicode encoding. In Unicode and the ASCII or Latin subset,

lowercase letters of the English alphabet are actually considered larger than the uppercase letters because the uppercase letters are earlier in the encoding, and therefore, have smaller numeric values. Within a given set of letters, the earlier letters are less than the later letters. For example, lowercase a is less than  lowercase g, since they have the Unicode decimal values 97 and 103 respectively. Since 103 is greater than 97, g is greater than a. For the names Jon and Joe, the first and second characters match. So, the comparison would continue. Then, when we get to the last letter in each, the n is greater than  e, they're different. So, Jon is greater than Joe. Pretty cool. Now, how does that translate to string methods equals and compareTo? Well, equals returns a Boolean value. It returns true if both strings match exactly. CompareTo returns an integer. If we have two strings, str1 and str2, and call str1.compareTo(str2), then it returns zero if they're equal, it returns a number greater than zero if str1 is greater than str 2 and returns number less than zero if str 1 is less than str 2. Hopefully, this makes more sense in just a minute. 

Sometimes, it's extremely useful to just jump in and write some code in order to understand it. If you haven't anticipated it and already done so, make sure to first create a Section5-Projects  IntelliJ project. Now, let's create a StringMethod1 Java class to work with the methods we just mentioned. So, we'll go here, New, Java Class, and I will call it StringMethods1. Here we go, public static void main(String[ ] args), and there we go. So, let's fill this in and look at some of the methods that we just discussed. So, we need some strings to work with. So, I'll put my name in string name and I need a name that's exactly the same because we're going to check some equals and do some stuff with that. So, name three is  Rob Percival.

So, we've got the length and charAt methods we can work with first. So, let's start with those. So, we got < name.length(). Just like you've got length property on the arrays, you've got to a length method on strings. And again, you can think of a string as an array of characters. It's the end of the for loop. So, System.out.print(),  no, it's not print line. I'm just doing print, name. and now charAt, so I want the character At. And then we'll just put a space in between. We could use a tab or two spaces or comma space or whatever you want really. And System.out.println() at the end to provide us with a little extra padding there. Now, let's talk about the equals and compare two methods. For reference types, the equal to operator, the double equals, like that, means to compare the content just like it does with primitive types. It always

compares the content, you can't change its behavior. Now, what is the content or the contents of a reference type? It's not the same as it is for a primitive. For a reference type, it's the address of the object in memory. So, how would we compare the actual string objects or the contents by so-called lexicographic order? Kind of alphabetic order, if we were just dealing with the English characters. So, we use the equals and compare two methods. So, when you compare strings in Java, don't use the double equals. if(name.equals(name2)), so this will be the two strings with John Baugh in them. We have System.out.println we'll say ("Names are equal.") else System.out.println("Names aren't equal.") Here we go. Now, let's do some compareTo. So, we're going to say is name greater than name3. In other words, is John Baugh greater than  Rob Percival?

So, if (name.compare.To()), we have name which has John Baugh in it being comparedTo name3, and we're testing is that greater than zero. I'll explain this in just a minute. So, you have name, which is John Baugh and you have name3 as the argument, that's  Rob Percival. What this is saying is,  is name John Baugh greater than lexicographically the  Rob Percival? Now, this returns an integer, if the integer is equal to zero, then the two names are the same. If it's greater than zero, that means that the name you called it on is lexicographically greater than the name that's passed in as the argument. If it returns less than zero, that means the name you called it on is less than the name that you passed in as the argument. So, hopefully, that makes more sense than it did when I was just talking. And then, we've got the else. So, I shall do this one first I guess, it doesn't really matter. But we'll say name greater than name3 just to print it out, and then this one we're going to say name <= name3.

We have to consider the equals because if this is false right here, we can't assume this is greater than because it could actually be or, I'm sorry, less than it could actually be less than or equal to. All right. So, let's run this, of course, and give it a test right click. We're going to Run the main method on StringMethods1, and we have the characters being printed from our first little set of code or bit of code up here. So, it prints out each of the characters individually with a space in between. That's why it prints John Baugh like this. And you'll notice there's two spaces in between John and Baugh. That's because one of the characters is actually one of the, is the space. So, a space takes up just as much memory as any other encoded character. We tend to think of it as blank or empty but computers have to encode spaces as well, just like new line characters. So, names are equal is what this says because the name.equals(name2). Now, name is John Baugh, name2 is also John Baugh, they have the exact same characters.

So, by using the equals method, not the double equals, we don't want to do that, that can give sometimes bizarre solutions that we wouldn't expect, or normally should not work right. Now, what happens here is we're using compareTo, which doesn't just return a Boolean like equals does, it actually tells us which one is greater or less than the other. And in this case, we found out that John Baugh is less than or equal to  Rob Percival. Because that's what is in name3. All right. So, to do a little review, we can see that the methods are working quite well. We know that name and name2 are equivalent because they contain the exact same string contents. When we compare name to name3, we see that John Baugh is being compareTo  Rob Percival. The comparison initially compares the first characters in each, since the R in  Rob is later in the alphabet than the J in John, compareTo can immediately return a value less than zero to indicate that John Baugh is less than  Rob Percival. So, we don't even have to go to the next character to test it out.

Before we move on, you guessed it. I have a challenge for you using this same StringMethods1 file. I would like you to add a string to the file with your own name in it and compare it to one of the other strings that we have, you can pick which one you want to, such as,  name  or  name3 strings, to see if your name is greater than, equal to, or less than mine or  Rob's names. So, pause the video, come back when you're done or if you need some help. How did that challenge go for you? Were you able to complete it? Let's work on it together. Since my name is already up here, I'm going to actually add another name right here. String, we'll call it challengeName and I'm going to call, I'm going to make it the name Mortram, Ed Mortram. And then, down here, let's do the compareTo. So, we'll say System.out.println(), we'll say ("Comparing for the lecture challenge") Right there. And then I'll say, if(challengeName.compareTo(name), if that is, rather greater than zero, we will know that the challenge name. And this time I'll, let's actually print the names instead of just printing out name  one or challengeName literally, I'm going to say, (challengeName and then we'll say, "is greater than", and then we'll say, name)

than else, I'm going to say, System.out.println(). I'll say (challengeName +  "is less than or equal to" + name). All right. Let's run this to test it and see what we get. We're paying attention mostly to the stuff that's going on below. So, let's right click that file and Run it again. And we're ignoring most of this other stuff and we see that Ed Mortram is less than or equal to John Baugh. It compared the first character E and J are both capital. So, they're in the same kind of subset, but E comes before J, so J is later. So, its Unicode character encoding has a larger integer value. Awesome work, everyone. Your output might be different depending on your name, of course, in the next lecture, we'll take a look at some more of the useful methods of strings. I'll see you there.

 

About the Author
Students
1364
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics