top
upGrad KnowledgeHut SkillFest Sale!

Search

Java Tutorial

What is Java Hello World Program?  "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). Java “Hello, World!” Program Example 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! Internal working of Java “Hello, World!” Program 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. Points you should remember always Each valid Java application must have a definition of a class (which matches the filename). The main method must be within the definition of the class. From the main function, the compiler starts executing the codes. 1. Comments in java 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: Single – line comments. Multi-line comments. Documentation comments. Single line comments 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");   }   } Multi-line comments 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*/  }   }                                                                                           Documentation comments 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*/ 2. Rules for naming conventions in java 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: Every identifier in Java must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). Identifiers may have any combination of characters after the first character. Legal identification examples: age, $salary, _name, _1_value. Illegal identifiers: 123ahgc, -lastName. A class name should start with a capital case letter and use camel casing for long names. For example, MyFirstClass It is impossible to use a keyword as an identifier. Identifiers are case sensitive. The name of the object should begin with the letter of the lower case and the long names should use camel casing. For example, myFirstClass. 
logo

Java Tutorial

Java Programs

  • What is Java Hello World Program?  

"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). 

  • Java “Hello, World!” Program Example 

class HelloWorldpublic 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! 

  • Internal working of Java “Hello, World!” Program 

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. 

  • Points you should remember always 

    1. Each valid Java application must have a definition of a class (which matches the filename). 
    2. The main method must be within the definition of the class. 
    3. From the main function, the compiler starts executing the codes. 

1. Comments in java 

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: 

  1. Single – line comments. 
  2. Multi-line comments. 
  3. Documentation comments. 
  • Single line comments 

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");  
}  
} 
  • Multi-line comments 

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*/ 
}  
}                                                                                           
  • Documentation comments 

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*/ 

2. Rules for naming conventions in java 

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: 

  • Every identifier in Java must begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). 
  • Identifiers may have any combination of characters after the first character. 
  • Legal identification examples: age, $salary, _name, _1_value. 
  • Illegal identifiers: 123ahgc, -lastName. 
  • A class name should start with a capital case letter and use camel casing for long names. For example, MyFirstClass 
  • It is impossible to use a keyword as an identifier. 
  • Identifiers are case sensitive. 
  • The name of the object should begin with the letter of the lower case and the long names should use camel casing. For example, myFirstClass. 

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

protective orders in virginia

Keep sharing blogs like this one; they are quite good. You have given everyone in this blog access to a wealth of information.

Johnfrance

Thank you for your valuable information.

sam

Thank you for this wonderful article!

Belay Abera

This article was really helpful to me, thank you!

dfsdf

super article!