Learn about how a Java program runs, and write your first program in this video. Kathryn helps you write a simple print statement and run it in IntelliJ IDEA.
- [Instructor] Let's create our first Java file. To do that, we're going to left click on this source code folder and we are going to create a new Java class and this Java class we're going to name it main. We'll learn more about what class and instance and all these technical terms mean later in the course, but let's go ahead and just start going with the code so we can see what really happens in Java. So we're going to click OK and here we have our first class in Java, our first file in Java, and notice it does have that .java file extension and that just means it's a Java class file.
You'll notice over here in this pane, we'll be able to type and write our code. This is our code editor and it's where we're going to write our programs. Now in order to write and execute code in a Java program, we need to have a main function. This is what Java automatically runs when we execute a program. And so in this Java class, this main class, we're going to need to create this main function so we're going to go public_static void main is going to be the name of the function main and then we're going to have one parameter, a string array, and this parameter is going to be called args.
We have a lot of technical terms here, public, static, void, main, and we'll learn more about what these mean later, but for now just know this is a function called main and it has an input called args. Now anything we put between these two curly brackets is going to be executed as part of the program. For JShell, we didn't need to write all of this class stuff because it was a read-eval-print loop tool in the shell and it allowed you to evaluate items one at a time. For a regular Java program, there's a lot more you have to write just to get started.
For our first program, let's try printing something out in the console. The console is where a user might interact with the program. So on line four here, we're going to write System.out.println Hello World in quotes and then we're going to add a semicolon at the end. Here, system is a class, out is a static property of that class, and println is a function. That's a lot to take in and we'll learn all the details throughout the course. But simply put, this is how we print stuff in the console.
This println function takes a string or a list of characters as input and Hello World is the input we have given it. This may seem intimidating, but using dots with system and out are just a way we get access to this println function which allows us to print stuff to the console to the user. Now our program is ready to run so we'll left click the main class here so going to our source code folder and then hitting main. We're going to go ahead and run this run main.main so running that main function within the main class.
And if you ran it with me, you'll notice a window popped up at the bottom of the screen containing our Hello World output. This window is called the console which is just a text display window where the user might see messages and here we're printing out the message Hello World. Let's try printing a name. My name's Kathryn so let's see if we can write System.out.println Kathryn and then remember the semicolon. Those are essential in Java. It's part of the Java language as a whole.
Hitting run again, now we have Hello World and underneath it my name Kathryn. Now like I said before, you might be noticing some of these random semicolons. In Java, we use semicolons at the end of the every execution statement such as print lines and these are print lines here, each of these lines on four and five. These semicolons are necessary because they separate execution statements for the compiler. Now we'll be using the console a lot in this course to output different values and see the results of programs we write.
Before moving on, try printing out some more values in the console to really get familiar with it.
- Downloading Java 9 and choosing an IDE
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Working with inheritance and interfaces
- Learning lambda