Java 8 | String Method

Student Kim
Buzz Code
Published in
4 min readJan 6, 2021

Method — A block of code that performs specific task.

Hi guys! Welcome to another Java session with me. Today I’m going to talk about the methods that we can use on String in java. Let’s get this started!

[Method]

I’m going to talk about method deeply in the future session, so for now all we need to know is how it works. Method is a block of code that performs specific task, followed by parentheses() . In the parentheses, we can put parameter which is a passed information. Some method need parameter, but some don’t. And some of the method can return data, but some don’t.

[String]

As we know, we can put collection of characters in the variable that declared as String. It has a common with array, that each character will be stored in each index of the String. For instance, let’s say there’s a String says “it is.”

As you see, each character is stored in the index of the String a which is started from 0 like array. Then How do we take each character out of the String one by one?

[String Method]

1) CharAt()

That’s when we use CharAt() method. This method can return the character at the specified index in a String.

Like above, you can call the method with the . after the variable. I put 4 in the parentheses as a parameter of charAt method, it means I am going to print the character at the index 4 of the String a. You can see the character s came out on the console. Then how can we print the whole characters out of the String a ? We can use the for statement just like we did with the array.

2) length()

I put the variable i starts from 0 and made it get bigger as long as it’s smaller than the length of the String a. As you can see the length() has parentheses as well, so it’s another method! length() method doesn’t need a parameter, but it does return the length of the String as a int data type. Here a.length() will be 6, cause the variable a has six letters includes space and the dot.

3) equals()

That’s right, you can easily assume that the Equals() method can tell whether two Strings are same or not.

And you can see it’s return type is boolean on the Template Proposals, when you type it in. And it needs parameter! And here we need to put the variable that we’re going to compare with another as a parameter.

I printed a.equals(b) out and you can see the false on the console.

But some of you might found out it also works when we use comparison operator ==. Then why can’t we just use the == ? Because Unlike the other data types that stores the value of the data, String stores data’s memory address. And the == compares the stored information, so for the String it compares each String’s memory address, and equals() compares String’s actual value.

So when I declare new String like above, although they have same values, but it says they’re not same since they have different memory addresses.

So if I changed == into equals() it works well again. So it’s better to use equals() method when you compare Strings.

And also we can distinguish data types that store memory addresses from data types that store actual values by the first letter of the data type. If it starts with the lower case letter, it stores actual value. And if it starts with the upper case letter, it stores memory address!

That’s all for today guys. There are some more methods for String but I’m gonna bring some practice questions that you can solve only with the three methods that are mentioned above. Thank you for reading my post, see ya!

--

--