Join David Gassner for an in-depth discussion in this video Hello World, part of Java 8 Essential Training.
- For my first Java programming exercise, I'll create a Java source code file, using a simple text editor. On Windows, I can do this using Notepad, or any other text editor I like. If you're working on Mac and you don't already have a text editor installed, you might try TextWrangler, a free text editor from Bare Bones Software. To declare a Java class, start with the keyword public. That's called an access modifier, and it means that the class I'm about to declare is available to the entire application.
Then add the keyword class. And then the name of the class, which I'll simply call Main, with an upper case M. All Java class names should start with an upper case initial character. Next, add a code block consisting of a pair of braces. In most Java coding examples, you'll see the opening brace appear on the same line as the preceding keywords, and then the closing brace should go down on the next line. And can align with the first keyword, such as public.
Now anything I declare inside this code block will be a part of the class. To run the class from the console, I need a method that's called main. And there are a few keywords I need first. First, the keyword public, which again is an access modifier. Then the keyword static. That's a keyword that means that this is a member of the class that can be called from the class itself, as opposed to an instance of the class. If that doesn't make sense, don't worry.
We'll talk a lot more about static versus member methods later on. Then the keyword void, which means that this method won't return any value. And finally the name of the method, main. This is a required method name. All method names should start with an initial lower case character, and that's how you can distinguish the identifier say, of a class named Main and a method named main. Next is a pair of parentheses that declares arguments or parameters that can be passed into the method.
The name of the method and its arguments make up the method signature. And the required main method signature receives an array of string values. We'll declare an array of strings, with the keyword String, with an upper case S. That means it's a class identifier. Followed by a pair of brackets. And that means there's more than one string. It's an array. The name of this argument can really be anything you like, but it's typically named args.
I'll add the closing parenthesis for the end of the method signature, and then I'll add another code block. And I'll fix up the tabulation to show where the method starts and ends. Now, when I run this class, the Java Runtime will look for the main method and run it automatically. Any code I put within the main methods code block will be executed. And I'll add a single line of code that looks like this. System.out.println, for print line.
And then I'll pass in a string literal value. That's a string, wrapped in double quotes. And I'll set the string to ("Hello from Java!") After the closing parenthesis, I'll put in a semi-colon. That's the end of the statement. Now I'll save my file. I'll press Control S in Notepad, or if you're working on Mac, you can press Command S. Then navigate to the folder where you want to save the file. I'm placing it in the HelloWorld folder, under the subfolder for the current exercise.
I'm going to set the file name as Main.java. The first part of the file name must match the class name. And it is case sensitive. So this value, Main for the class identifier, matches the beginning of the file name, Main, here. And the file extension must be .java. On Windows, if you're working in Notepad, change the type to All Files. And that'll guarantee that you don't add a .txt extension at the end. Then Save the file.
Now I'm ready to compile and run the Java class. I'll go to a Command Prompt. Now, switch to your Exercise Files folder. On my system, I'll go to the Desktop, and then to Exercise Files. Then to Ch03, and then 03_01. And finally the subfolder, HelloWorld. On Mac you can do the same thing, but use forward slashes instead of back slashes. I'll list the contents of the directory and show that Main.java is here.
If you're on Mac or Linux, use LS instead of DIR. Now, I'll compile the file. I'll type javac and then the full name of the file, including the java file extension. And after a moment, the Command Prompt comes back. If you don't see any errors, that means that the file compiled correctly. And you can list the contents of the directory again. You should see a new file there called Main.class. That's your compiled class file.
Now I'll run the class. I'll type java, and then the identifier of the class. When you're running the class, don't include the .java file extension. And I get back the string. Hello from Java! So I've created my first Java class using a simple text editor and I've compiled it and run it from the Command Prompt.
- Understanding the history and principles of Java
- Installing Java, IntelliJ IDEA, and BlueJ
- Creating a Java project
- Working with variables, values, and expressions
- Working with object data types
- Building, comparing, and parsing strings
- Debugging and exception handling
- Creating loops and reusable code
- Passing arguments by reference or value
- Using simple and complex arrays
- Creating custom classes
- Understanding inheritance and polymorphism
- Managing files with Java libraries
- Documenting code with Javadoc
- Packaging classes in JAR files