Hello and Welcome to Java Blog #1, Java: Hello World. This blog will introduce the print statement in Java.
Before beginning on the print statement, there are a few things that must be done first. You need to define a class and the main() method inside this class. These things will not be covered in this post, just used in example. They will be introduced in detail in a later post.
public class HelloWorld {
public static void main(String[] args) {
* Code would go here!!*
}
}
As you can see the first line is the defining of the public class, and the class name is Hello World. In Java, a program can be a single file, or multiple, but in every single file, at least one class name must be the same as the file name.
The file name is followed by an opening curly bracket. In the classes, main() methods and some other things, the curly brackets are like the ends of the code, where the code would go in between. The ending curly bracket that corresponds to this one is at the bottom.
Inside the public class is the main method. The static void part is just to define it, for now, just copy this. After 'main' there are parentheses containing 'String[] args.' These are basically just placeholders, so you will need to add these. Their purpose and other things will be explained another time.
You code, in this case, the print statement, goes inside this main method. In Java, you would type the print statement as such:
System.out.println(*Whatever you want to print*);
It is the System.out.println, followed by parentheses containing what you want to print. For example:
System.out.println("Hello World!");
Output:
Hello World!
System.out.println(1);
Output:
1
Inside the parentheses, notice that the first example has quotes and the second doesn't. It depends on the types of what you want to print whether or not these quotes are needed.
Also, notice the semi-colon at the end of the line. This semi-colon is very important and is part of Java syntax. It will appear at the end of every statement, or line of code that has a separate function, in Java. Above, you can see that the class defining and main() method do not have this semi-colon. Some thing, like these do not.
Thank you for reading Java: Hello World! Please leave any comments or questions in the comments section!
Comments
Post a Comment