Copyright

Java's 'Hello World': Print Statement & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: If Statements

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 What Is Java Programming?
  • 0:50 Hello World Java Program
  • 1:06 Hello World Java Components
  • 2:38 Java Syntax
  • 3:21 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Sudha Aravindan

Sudha has a Doctor of Education Degree and is currently working as a Information Technology Specialist.

Hello World is a simple Java program, for many the first they learn, that teaches syntax and components of the Java programming language. In this lesson, we will learn how to write your very first computer program using the Java programming language.

What Is Java Programming?

Just as a quick refresher, Java is a programming language that allows you to provide instructions to a computer, and with which you can create computer programs. The computer will then compile the instructions, or program, that you wrote; inform you of errors, if any; and if there are no errors, will execute the instructions in your program.

In order to be able to write and run Java programs, you first need the Java compiler installed on your computer. Once you have the Java compiler successfully installed, you can write, compile, and run a Java program.

Compiling code means using the Java compiler or program that translates the code that's written by humans in Java language to machine or computer language. All programming languages have to be compiled so that the computer can read the code and execute the instructions.

Hello World Java Program

Once you have the Java compiler successfully installed, open a text editor (example, Notepad) and type in the following:

/*
* The HelloWorld Java program
* prints "Hello World!" on the computer screen
*/
class HelloWorld {
  public static void main(String[] args) {
   System.out.println("Hello, World!");
  }
}

Now compile that program using the command:

javac HelloWorld.java

Now you will see that the computer has printed:

Hello, World!

on the display monitor.

Hello World Java Components

Let's take a look at the components of this program one at a time:

1. Comments

This /* … */ is used to represent comments in the Java programming language. The Java compiler ignores everything between /* and */. The comments are only meant for the human reader to understand what the program is doing.

2. Name of the Java Program

In this Java program, after the comments you see the statement:

class HelloWorld {

This statement is called the main method and should be included in every Java program. This tells the Java compiler that this is the beginning of the Java program. The program then executes all the statements following the main method. The words 'public' and 'static' can be interchanged, (as you see here) but the norm is to write 'static' following 'public.'

static void main (String[] args) {
}

The main method has to be written as is, and no words can be omitted. So for example, if you have a main method like this one appearing here:

Public static void main()

This will not compile and you'll get an error because this phrase here

(String[] args)

is missing within the parenthesis.

Also if you omit the word 'main,' the Java compiler will not know where to start and what the beginning of the program is, and so will generate an error. So for example, the following Java program appearing here will not compile because there is no 'main' following 'void:'

class HelloWorld {
  public static void(String[] args) {
   System.out.printlin("Hello, World!");
 }
}

3. Print method

If you look at the Hello World program, you will see that following the main method there is one statement:

System.out.printlin("Hello, World!");

This is the statement that prints or displays the words

Hello, World!

on the computer screen.

If you wanted to print

Welcome, World!

you would modify your Java program to say this code here:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support