Skip to main content

Simple Java program to print "Hello World"

Let us write a simple Java program to print "Hello World".

/*
* HelloWorld.java - simple java program to print "Hello World"
*/

public class HelloWorld {
public static void main(String args[]) {
// print "Hello World"
System.out.println("Hello World");
}
}


Above is the simple Java program which would print "Hello World" on execution.  While saving the Java program, please make sure that the file has .java extension and the name of the file matches with the name of the class that has main() function.  We have stored the above source code in HelloWorld.java and the file names matches exactly with the name of the class in which main() function resides.

Let us compile the above Java program.  Basically, Java compiler would take Java source code as input and provide us bytecode version of the input Java program as output.  As a result of compiling HelloWorld.java, we will get a class file named HelloWorld.class which contains bytecode of HelloWorld.java.

d:\Java>javac HelloWorld.java

Java Virtual Machine would take the bytecode as input and prints the program output on the console.  Let us execute our Java program by providing our class name HelloWorld as command line argument to Java command or Java application launcher.  As a result of program execution, "Hello World" is printed on output screen.

d:\Java>java HelloWorld
Hello World

/* */ Multi-line comments using which we can be used to provide comments in multiple lines to explain code flow or programming logic. These comments are ignored by the compiler
// Single line comments
public access modifier which helps to control the visibility of classes, methods and other members
class HelloWorld class is the Keyword used to declare class. HelloWorld is the class name
main() Method where the program execution begins
static static is the keyword which can be used on variables, methods or blocks. static methods can be called without instantiation. Here, main() is a static method
void Return type of a function. void is used as return type when a function has nothing to return
System.out.println() System is the built in class. out is the object and println() is a built-in method
{ } open and close brace

Comments

Popular posts from this blog

scanf() and printf() functions

Scanf() function: It is used to read input from the keyboard. int scanf(const char *format, ...); For better understanding, lets rewrite the above as follows. scanf("format specifiers", &v1, &v2, &v3); v1, v2 and v3 are the input variables whose value needs to read from the keyboard.  We need to pass address of the variable as argument to store the input values from keyboard. Here, ampersand(&) represents address. &v1 - address of variable v1 Input value for v1 will be stored at address of variable v1 Format specifier can be any of the following %u  - unsigned int %d  - int %x  - hexadecimal %o  - octal %f  - float %lf - double %c  - char %s  - string scanf("%d", &v1); - inputs integer value scanf("%x", &v2); - inputs hexadecimal value scanf("%f", &v3); - inputs float value printf() function: It prints the given data on the output screen. printf("format specifiers", v1, v2,..vn); Here, format specifier ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Multidimensional arrays in C

Multidimensional array is also known as array of arrays.  C allows user to write arrays with one or more dimensions.  An array with more than one dimension is a multi-dimensional array.  Two dimensional array is a simplest form of multidimensional array.  Below is the general form of multidimensional array. data_type  array_name[size1][size2][size3]..[sizeN]; Consider the following example int arr[2][2][2] = {   { {1, 1}, {1, 1} },                                   { {1, 1}, {1, 1} }                               } ;   The above statement shows the declaration and initialization of multidimensional array(3d array).  Total number of elements in an array can be obtained by multiplying the size value in each dimension. int arr[2][2][2]; 2 X 2 X 2 = 8 elements in the array arr And multidim...