Tuesday, January 12, 2021

simple java program to print hello world

 simple java program to print hello world.

class Sample {
public static void main(String []args) {
      System.out.println("Hello World"); // prints Hello World
   }
}
In java programming file saving, file compiling, and run is different from any other languages. Therefore follow these step for the writing program:-
Step 1:- First open the notepad and add the code as above.


Step 2:- Save the file as Sample.java
Step 3:- Open the command prompt window and go to the directory where you saved the file. Assume it is in C:\>
then type javac Sample.java and press enter for compile. If there are no errors in your code then it cmd take to the next line.
Step 4: Then type java Sample then it gives output hello world.
3 and 4 show step 3 and 4 respectively
Let's discuss our code:-
The first line of our code is 
class Sample{
The word class is a keyword to define a new class and Sample is the name of the class. The class definition must begin with opening curly braces({) and end with closing curly brace(}). The rest of the things defined inside these braces are called members of the class.
The next line of the code in our code is
public static void main(String []args)
This line tells the computer that this is the main part of the program or also called the main() method of our program. At this point execution start by calling the main() method.
The keyword public indicates it is globally accessible.
The keyword static ensures it is accessible even though no object of the class exist.
The keyword void indicates it does not return a value.
The next line of our code is 
System. out.println("Hello World"); // prints Hello World
The system is the name of a standard class that contains the object that encapsulates the standard I/O devices for your system--the keyboard for command-line input and command-line output to the display. It is contained in the package java.lang, so it is always accessible just using the simple class name System.
out is the object that represents the standard output stream- the command-line on your display screen. out is the static variable. This means that out exists even there is no object of class System. System. out-references the out member of the class.
println("Hello World"), calls the println() method that belongs to the object out. It outputs the text string that appears between the parentheses to the command line.
// it shows comments in our code.





No comments:

Post a Comment

Data Types in Java

 Data Types in Java Data Type in Java is defined as that specifies the size of memory allocated for a specific variable and which type of va...