Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As you learn how to program in Java, keep this idea always in mind, everything is an object. As you design your applications, you'll be placing your code inside class definitions and wrapping that code inside functions known as methods. Let's take a look at a very simple application the Hello World! application I've shown previously. This time I've given the starting class a different name SimpleApplication. Notice that the main method though looks the same as before. That syntax is required, and as stated previously, the executable code is inside the main method.
It outputs the string "Hello World!" to the console. You can diagram your Java applications using a syntax known as UML or Unified Modeling Language. This diagram shows that the name of the class is SimpleApplication and that it has a single method or function named main and it returns void which means it returns nothing. It doesn't return any value at all. Now, from this simple class, you might not get the idea that everything is an object. So let's expand the application a little bit to two classes.
In this version of the application there are two classes, the starting class is still called SimpleApplication. It's the starting class because it's the one with the main method, and as the application starts up, the main method is called and the code in that method creates an instance of another class called Welcomer. That class is shown at the bottom of this screen. Shift your attention down to the welcome request. It declares a variable named welcome outside of any methods. We call those either instance variables or fields.
In this case, there is an instance variable named welcome, it's a string, and it's private to the class which means that it can only be read by the code within that class. Its value is Hello. When, the function sayHello is called from that class, that class is responsible for outputting the string to the console. From the main application we create an instance of that class using the syntax Welcomer, that's the uppercase version, that's the data type and then the lowercase version is the name of the variable and we say that it's equal to the new instance of the Welcomer class that's an object and the object can have methods and properties.
We call the object sayHello method and the result is to output the string to the command line. Once again, you can diagram this application using UML, in this case there are two classes SimpleApplication and Welcomer. This first class simply has the main method; the second class has both an instance variable or a field and a method named sayHello. Once again sayHello returns void or nothing, but it takes the action of outputting a string to the console. In Java, we say that everything is an object because it's very common to create an instance of class to execute some code and that code will be wrapped inside the class.
As we actually get into coding some classes, I'll describe what's an object, what's a variable, and what's a field. Here is another way in which Java sees everything as objects. You can declare a String variable by declaring the data type String and then the variable name, in this case the variable name is welcome and then you can say that that variable has a value of a string wrapped in double codes, but in fact, this is shorthand. Here is the bit of code that's the longhand version. We declare the data type, we say what the variable name is, but now we say that we're creating a new instance of the String class and we pass in the string that we want.
This is the longhand version and these two bits of code are exactly functionally identical. They do the same thing. They create an instance of the String class and so welcome is an object. But you can go further with this idea, because within a string object there is actually an array of individual objects, one for each character. So you can say that welcome is a String, Hello or you can say that welcome is an array of six object, H-e-l-l-o-! And here is a bit of code that breaks it down even further.
I'll start off with an array of characters. char, bracket, bracket means create an array of characters. The name of the array is chars, C-H-A-R-S, and then the following syntax with the individual characters each wrapped in single quotes and then all then all six characters wrapped inside braces, means create an array. I can take that array of characters and wrap them inside a new string called s and now once again I have either an array of six objects or a single object the String s. When you start seeing everything as an object, you see that you can break things down to their smallest components in Java; a String is really an array of characters.
A primitive numeric value is an individual value, but a complex object can reference many objects. By breaking things down into small pieces in Java, it makes it easy to find the code that you want to expand on or fix. The output of this code would be simply Hello! Because the String class is a complex object, it can encapsulate or offer all sorts of functionality and in fact the String class, which in longhand is java.lang.String includes many, many methods.
This is a short list of just a few of the methods that are a part of the String class. The charAt method lets you pass in a numeric value and get back the character in the position in the array that's a part of the String class, you can use compareTo to compare one String to another, the concat to concatenate Strings together and so on. We'll be looking at these and many more methods of the String class later on, but my point is that a string is not simply a string, it's an instance of a class and that gives you access to all the functionality that's built into that class as a part of the Java Programming Language and its core runtime, the Java Class Library.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97508 Viewers
61 Video lessons · 84747 Viewers
71 Video lessons · 68923 Viewers
56 Video lessons · 101377 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.