课程: Learning Java 17

Write your first Java program - Java教程

课程: Learning Java 17

Write your first Java program

- [Instructor] We're ready to write our first Java program. Let's create a Java file. We'll left click the SRC folder and hover over New. we'll create a new Java class. And we'll name it Hello World. Another option is to select File from the navigation bar, hover over New, and create a new Java class this way. Java is a verbose programming language. So there's some boiler plate code that we add with every program. When we say verbose, we mean there are lots of words we have to write compared to other languages in order to get a simple program to work. For beginners, I think this is a good thing, because it reveals concepts you may not have known and it makes what you're trying to do very straightforward and clear. Everything is written out. Every Java program has to have a class. And in fact, all Java code must be inside of a class. That's why this file is pre-populated with public class and then the name of the class, Hello World. The idea of Hello World is a bit of a tradition in computer science. When you're first learning a new language or framework or technology, all you want to do is make the program output Hello World. And that's exactly what we'll be doing today. This main pane is called the code editor, and it's where we'll edit our code. If we exit out of the file, we can easily access it again inside the SRC folder. We'll double click it and it reappears. This allows us to access and edit our code pretty easily. Let's add some more Java code in between the curly brackets. Public static void main string args, and more curly braces. This might look a little overwhelming, but don't worry. All we need to know about this piece of code is that it's a main function. It's the entry point to our program. This means any code between these curly brackets will be executed when we run the program. Everything we've written so far is boiler plate, or stuff we must have for any Java program to run. The thing that will make our program unique is what we add in those curly brackets. With the Hello World program, we want our program to output the text Hello World. We can do that by adding system.out.println, Hello World, it's me, Kathryn. The system.out.println gives us access to the outputting or printing functionality of Java. In between the parentheses, we write out what we want to output. In this case, it's Hello World, it's me, Kathryn. Now you might be wondering what public means and how it relates to Java, but that's out of the scope for this introductory course. However, we will talk about what void and static mean in later chapters. Before we build and run our program, it's really important to check that the file name and the class name match. The name of the file is Hello World. And this must match the name of the class written in the Java code. Otherwise, we'll get errors. Our Java program is written. Let's build and execute our code.

内容