Get startedGet started for free

What are methods?

1. What are methods?

Welcome! I'm Jim, and I'll be your instructor for this course.

2. What you'll learn

If you took the previous course, you already have a solid foundation in Java. Here, we'll extend our Java knowledge by introducing three new key concepts. First, we'll explore methods, which let us group code into reusable blocks instead of repeating the same instructions over and over at different places in the code. Then, we'll move on to control flow. We'll learn how to make decisions in our programs using if statements and logical operators. We'll also dive into loops, which help us perform operations on individual values within data structures such as Arrays. These are skills that every Java developer needs, whether we're building small programs or large applications. Finally, we'll combine everything into a workflow.

3. What is a method?

Let's start with methods. A method is a block of code that performs a specific task. Instead of writing the same code multiple times, we define a method once and call it whenever we need it. Think of it like a coffee machine - once it's set up, we don't have to brew coffee manually every time. We just press a button, and it does the work for us.

4. Methods we've already seen

We've already used methods without realizing it. Many of Java's data types, like `Strings`, have their own built-in methods that make working with them easier. You might remember the `.length()` method, which tells us how many characters are in a String. Now, let's look at some other ones.

5. .contains()

The `.contains()` method checks if a `String` contains a specific sequence of characters. For example, if we have the word `"Java"`, calling `"Java" .contains("av")` returns `true` because `"av"` is part of `"Java"`.

6. .charAt()

Another useful method is `.charAt()`, which lets us get the character at a specific index in a `String`. If we call `"Java" .charAt(1)`, it returns `'a'` because Java starts counting from 0.

7. .substring()

We can also extract part of a `String` using `.substring()`. This method allows us to take a section of a `String` by specifying a start and end position. When using this method, the start position is included and the end position is excluded. For example, `"Java" .substring(1, 3)` gives us `"av"`, starting at index 1, the start position, and stopping before index 3, the end position.

8. .equals()

Finally, there is `.equals()` that allows us to compare the two Strings. Here, we are comparing `word` with `“Java”` and the result would be `true`. If we used the `==` operator, the situation is a bit more complicated and the result might not be what we expect, so it's better to use `.equals()` method.

9. Summary

Methods make Java more powerful by letting us write more efficient, reusable code. Here is a summary of what you've learned.

10. Let's practice!

Now that you've seen some useful String methods, let's jump into the first exercise and start working with them!