In the previous chapter, we created a Java file named Main.java and printed "Hello World" to the screen using the following code:
public class Main {
public static void main(String[ ] args) {
System.out.println("Hello World");
}
}
Every line of Java code must be contained within a class. In our example, the class was named Main. Always begin a class name with an uppercase letter.
Note: Java is sensitive to the case: "MyClass" and "myclass" have different meanings.
The filename must correspond to the class name. When saving the file, include the class name and ".java" at the end. For the preceding example to run on your computer, Java must be properly installed. The output ought to be:
Hello World
The main() method is required, and you will see it in every Java program:
public static void main(String[ ] args)
Any code contained within the main() method is executed. Refrain from being concerned with the keywords preceding and following the main. As you read this guide, you will gradually become acquainted with them.
For the time being, remember that every Java program has a class name that must match the filename and that every program must contain the main( ) method.
Inside the main() method, we can use the println() method to print a line of text to the screen:
public static void main(String[ ] args) {
System.out.println("Hello World");
}
Note: The curly braces indicate the beginning and end of a code block.
System is a Java class with useful members such as out, which stands for "output." The println() method, abbreviated as "print line," is used to display a value on the screen (or a file).
Concern yourself only minimally with System, out, and println (). Just be aware that you need both to print content to the screen. Note that each code statement must conclude with a semicolon (;).
You learned from the previous chapter that you can use the println() method to output values or print text in Java:
System.out.println("Hello World");
You can add as many println() methods as you want. Note that it will add a new line for each method:
When you are working with text, it must be wrapped inside double quotations marks " ". If you forget the double quotes, an error occurs:
There is also a print() method, similar to println( ).
The only difference is that it does not insert a new line at the end of the output:
You can also use the println( ) method to print numbers.
However, unlike text, we don't put numbers inside double quotes:
You can also perform mathematical calculations inside the println( ) method:
Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be executed). This example uses a single-line comment before a line of code.
Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by Java.
Multi-line Comments