Lesson Weekend

In the previous lesson we briefly discussed Java Objects and Methods, among other things. However, because these concepts are so foundational to Java, let's take a moment to explore them a little further before moving on.

Like many other languages, Java is object-oriented. In object-oriented programming languages nearly everything is an object. (There are a few exceptions to this, but we'll address them in the next lesson). Object-oriented languages are great programming languages for beginners especially, because there are only two primary concepts you need to know about: Things and actions you can tell those things to do. In Java we call these things objects and the actions you can tell them to do methods. Here's an example of an object and a method in our REPL:

> "programming is awesome".toUpperCase();
java.lang.String res0 = "PROGRAMMING IS AWESOME"

Here, our string "programming is awesome" is an object, and toUpperCase() is a method called upon that object to do something to it. This should be fairly similar to the manner you used methods and objects in Intro to Programming.

The response displayed after the method (ie: "PROGRAMMING IS AWESOME") is the result of the action taken by that object. We call it the return value. Like everything else, it is also an object. Again, this should be review.

Chaining Methods

Java also allows us to call multiple methods back-to-back, just like we've done in JavaScript. Let's take a peek.

For the purposes of this example, we'll pretend we accidentally included whitespace before and after our string. Thankfully, Java has a trim() method to remove unnecessary whitespace from the beginning and end of a string. It works like this:

> " programming is awesome ".trim();
java.lang.String res9 = "programming is awesome"

As you may have guessed, the return value of trim() is also an object. So, because trim() is just returning yet another object, we can chain methods together like this:

> " programming is awesome ".trim().toUpperCase();
java.lang.String res10 = "PROGRAMMING IS AWESOME"

The second method is called on the object returned from the first method. So, trim() is called on " programming is awesome ", which returns "programming is awesome" without white spaces. Then, toUpperCase() is called on "programming is awesome", returning "PROGRAMMING IS AWESOME". Again, this should be review.

Methods are Tied to Classes

Now, what happens if we try to call the toUpperCase() method on a number?

> 15.toUpperCase();

ERROR: not a statement

ERROR: ';' expected

ERROR: ';' expected

This error message states that we cannot call toUpperCase() on a number.

There are different "types" of objects, but in Java, the real word is class. "programming is awesome" is of the String class. The method toUpperCase(); is a method specifically meant for objects of the String class. Since 15 is not a string, we simply cannot use this method upon it.

Here's another term to get familiar with, if you're not already: instance. Every object is an instance of a class. "hello!" is an instance of String. If there were a class called Person we could have instances of it called mary, bob, and sam. They are instances of the class because they are specific, unique people, but each have the traits of a Person.

We can see what class an object is by calling getClass() on it, like this:

java.lang.Class res12 = class java.lang.String

Also, each class has different methods defined on it. We can't call a method from one class on an instance of another class, because it simply doesn't have that method defined on it. If we try, we'll receive an error. Sometimes, two different classes will each have a method with the same name, but they are actually different methods defined in different classes.

So, thanks to the getClass() method, we know "PROGRAMMING IS AWESOME" is an instance of the String class. Therefore, any String method can be called on it. And, if we take a peek at the Java documentation for the String class, we can see there's a large number of built-in methods available for strings. Cool!