Join David Gassner for an in-depth discussion in this video Java syntax and compilation, part of Java 8 Essential Training.
- All Java code is defined in classes. Each source code file defines at least one Java class that has the file extension .java. You can create these text files in any text editor, although I'll be using IntelliJ IDEA for most of this course. The compiler, javac, compiles your text-based code into bytecode that can be interpreted by the jvm, and then the java command runs that code. Here's a classic Hello World application.
The name of the class is Main, and so it's defined in a text file named Main.java. The first line is called the package declaration. Java classes are typically organized into packages. A package is a globally unique string that typically starts with your domain name in reverse domain order. So if my domain is david.com, I would start my package string with com.david to ensure globally unique identifiers.
Then if there's more than one class named Main in an application, I can distinguish them using the package. Next is the class declaration. The keyword public means that this class is available to the entire application and each source code file will contain one public class. The name of the class is Main, that's its identifier. And it implements a single method, also named Main. Notice that the methods identifier uses an initial lowercase character.
A console application always has this main method. It always has the three keywords, public, static, and void. Public means that the method is available to the entire application. Static means that this method can be called directly from the class definition rather than from an instance of the class. And void means that the method doesn't return any value. This required main method must also receive an array of strings as an argument or parameter.
It must be an array of strings. But the name doesn't necessarily have to be args, you can name it anything you like. Finally, the executable code is placed within the main method. This code is printing the string Hello World to the console and it ends with a semicolon, which is like a period. It means that's the end of the statement. Here are some critical Java syntax rules. Java is case sensitive, and all identifiers must be unique within their scope.
Because Java is case sensitive though, these three identifiers or variable names are seen to be different from each other, firstname in all lowercase, firstName with camelCase, and FIRSTNAME in all uppercase could represent three distinct variables or values. Also, in Java, white space doesn't affect how your code is interpreted. For example, in Visual Basic and some other applications, a line feed means that's the end of the statement.
In Java, a line feed is just like a space and a tab character. All three of those types of characters are collapsed during the compilation process into a single bit of white space. And so it's up to you to indicate where a statement ends with a semicolon character. This code for example says print this string to the console. And the semicolon character is required. Identifiers or names of your classes, variables, and other components can't use keywords.
The Java language implements a whole set of keywords that you should learn about. Keywords like for, while, assert, and others can't be used as your variable names, and if you try to do that, the compiler will reject them. Again, names of classes, methods, variables, and so on are known as identifiers. And all identifiers must start with an alphabetical character or an underscore character. You can't use numbers and you can't use other special characters.
There are some very strong naming conventions in Java and you should learn them and use them. Class names always start with an uppercase character. So MyClass starts with an uppercase M, but then can use other uppercase characters as needed to make the code readable. Methods, variables, and other members of a class always start with a lowercase character. So this would be a method called doSomething. It starts with a lowercase d. And it receives an argument or parameter that starts with a lowercase w.
If you were to type these identifiers with uppercase characters, your code would still work, but other Java developers would wonder what you were up to. And constants are always all uppercase. There is no constant keyword in Java. Instead, a constant is static and final. Static means it's a member of the class and final meaning once it's been set, it can't be changed. This is a string constant named FIRSTNAME that has a value of David, and because it's a constant, FIRSTNAME is in all uppercase.
These are some of the most critical syntax rules that you should know about as you start to program with Java, but of course I'll talk about many others as the course goes along.
- 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