ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » Java solved programs » Java basic programs

Java program to print 'Hello world' (First program in Java)

Learn: How to write, compile and execute our first program in Java? in this program we will print some text ("Hello World") on the screen.

Welcome! Now, you are in the world of Java programming. Here we will learn to write, compile and execute a java program.

1) Writing a java program

You can write a java program in any simple text editor like Notepad, Notepad++ or any IDE (Integrated Development Environment) which supports Java compilation and execution, but here we are talking about writing java program - So use any text editor, write your program (a basic program in given below) and save the file with extension .java

Here, we are saving it with "HelloWorld.java".

2) Compiling java program

In java compilation, it’s a process to convert text file (.java file/java program) to class file. Java compiler converts Java source code to the class file and produced output file will be "HelloWorld.class".

Note: public class name will be the class file name.

To compile java program, follow following syntax:

    javac HelloWorld.java

Here, javac is the name of Java compiler.

3) Executing/Running java program

Once you compile the Java program and if it compiled successfully, you can execute Java program to produce the output.

To execute class file, follow following syntax:

    java HelloWorld

Here, "HelloWorld" is the name of class file (it may be different)

ADVERTISEMENT

Program to print "Hello World" in Java

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

Output

Hello World!

Understanding the program

1) public class HelloWorld

Name of the public class, if there is no public class defined in the program, the class which has main() method will be class file after compiling the program.

2) public static void main(String []args)

main() method that calls when program is being executed. String []args - object of string array for compile line arguments.

3) System.out.println()

Method that prints the text and/or values on the output screen.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.