"Hello, World!" is a simple program that produces Hello, World! It is often used to introduce a newbie to Java, because it is a very simple and easy program.
Let’s see how the program of Java "Hello, World!" works.
Make sure that Java is properly installed if you want to execute this program on your computer. You also need an IDE to write and edit Java code (or a text editor).
class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
You need to save the file name as HelloWorld.java if you copied the exact code. It's because Java should match the class name and filename.
When we execute the code above, the output will be: Hello, World!
1. class HelloWorld { ... }In Java, each application starts with a definition of class. HelloWorld is the class name in the program and the class definition is:
class HelloWorld { ... .. ... }
Remember that for now, each Java application has a class definition, and the class name should match the Java filename.
2. The main method
This is the most important method. Each Java application must have a main method. The Java compiler starts from the main method to execute the code.How is it going to work? Good question, good question. We're not going to discuss this in this article, though. After all, introducing Java programming language to a newbie is through a basic program. We're going to learn the meaning of public, static, void, and how do methods work in subsequent chapters.For now, just remember that your Java application's main function is the entry point, and it's mandatory in a Java program. The main method's signature in Java is: public static void main(String[] args) { ..}
3. System.out.println("Hello, World!");The above code prints the Hello, World string to default output (your screen). Note that this statement is within the main function within the definition of the class.
Comments participate in a program to make the program more human-readable by placing the code details involved and using comments properly facilitates maintenance and easily finding bugs. Comments are ignored when compiling a code by the compiler.
There are three kinds of comments in Java:
A programmer at the beginner level uses single-line comments to describe the functionality of the code. It's the easiest comment typed.
Syntax:
//Comments here Example:
//Example of single line comments class Singlecomment { public static void main(String args[]) { // Single line comment here System.out.println("Single line comment above"); } }
Single-line comments can be tedious to describe a complete method in a code or a complex snippet as we must give ' // ' on each line. This multi-line comment can be used to overcome this.
Syntax:
/*Comment starts continues continues . . . Comment ends*/
Example:
//Example to show multi line comments class MultiLinecomment { public static void main(String args[]) { System.out.println("Multi line comments below"); /*Comment line 1 Comment line 2 Comment line 3*/ } }
This type of comment is generally used when writing code for a project/software package, as it helps generate a reference documentation page that can be used to obtain information about present methods, their parameters, etc.
Syntax:
/**Comment start * *tags are used in order to specify a parameter *or method or heading *HTML tags can also be used *such as <h 1> * *comment ends*/
All components of Java need names. Identifiers are called the names used for classes, variables and methods. There are several points about identifiers in Java to remember. Keep in mind:
Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.
Thank you for your valuable information.
Thank you for this wonderful article!
This article was really helpful to me, thank you!
super article!
Leave a Reply
Your email address will not be published. Required fields are marked *