• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Introduction » Hello World Java Example
Next →
← Prev

Hello World Java Example

By Dinesh Thakur

All Java programs usually start as text files that later use to create “class” files, which are executable programs. It means that Java programs can be written in any simple text editor such as Notepad.

Follow these steps to create a simple Java program that displays a traditional greeting.

1. Open a basic text editor like Notepad and type the following code exactly as here – you will create a class named Hello.

class Hello{
}

2. Between the two curly braces of the Hello class, insert the following code to create the Hello class’s main method.

public static void main (String [] args) {
}

3. Between the main method’s curly braces, add the following line of code that defines what the program will do.

System.out.println ("Hello World!");

4. Save the file in any convenient place and name it exactly like this: Hello.java – the finished program now looks like this:

hello java programFor a clearer understanding of each separate part of the program, we will consider them individually.

We’ll be covering the following topics in this tutorial:

  • Class Definition
  • Definition of Method main()
  • Contents of the main () Method

Class Definition

class Hello {}

The program name is declared after the class keyword, followed by a pair of curly braces. All program code that will define the Hello class will be placed inside these curly braces.

Definition of Method main()

The second line defines a function(method) and methods of main(), which a gateway or starting point for the program. each program a Java method starts from main() with the signature :

public static void main(String[] args)

The method must be declared as just illustrated , it should be public, static and void, must have a name and a main list of parameters must have a single parameter of type array String. Places of public and static modifiers can be exchanged. In our example, the parameter is said argument, but this is not optional parameter can have any name . most programmers choose a name args or argv.

If any of the above requirements is not met , the program will compiles , but will not be able to start and will give us a message error because it contained no starting point.

Contents of the main () Method

The Code line System.out.println(“Hello Java World”); will really work in this program. The text “Hello Java World” is a literal string of characters; that will be appear in output exactly as entered. Any literal string is written between double quotation in Java Programming Language because the string is an argument for a method, and arguments to methods are always appear within parentheses. The dot in System.out.println are used to separate the class, object, and method. The println() requires only one argument. The println() prints a line of output on the screen.

Anatomy of a Java Statement

In the statement System.out.println(“Hello Java World”);, out is an object. The out object represents the screen window.

The Parts of a Typical Class

The print() is similar to the println(). The println() insert a new line after the message prints. While the print(), does not insert a new line; it remains on the same line as the output.

Here is the java code for the program HelloJavaExample :

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

Hello Java Example

Compiling and running programs

Before running a Java program for execution, it must be compiled into a class file using the Java compiler, located in the bin subdirectory and has the name javac. It was previously described how to add the bin subdirectory to the system path so that the javac compiler can be run from anywhere on the system.

Follow the steps below to compile the program from the previous page.

1. Open a command prompt (terminal window) and change to the directory where you saved the Hello source file. java.
2. At the prompt, type javac followed by a space and the filename Hello. java with the source code and press the Enter key.

Compiling and running programsIf the javac compiler finds errors in the code, it stops and displays a message indicating the nature of the error — see below for debugging problems. If the javac compiler does not find any errors, it creates a new file with the program name and the .class extension.

When the compilation process ends, focus returns to the command line without any warning message, and the program is ready to run. The Java program interpreter is an application named java located in the bin subdirectory along with the javac compiler. Since this directory has already been added to the system path, the java interpreter can also be launched from anywhere.

Follow the steps below to run a program that was compiled using the procedure on the previous page.

1. Open a command prompt (terminal window) and change to the directory where the Hello.class program file is located.
2. Type java and the program name Hello, then press the key Enter.

The Hello program starts and executes the commands described in the statements of the main method. In this case, it outputs the string Hello, world! Once completed, focus returns to the prompt again.

The process of compiling and running a Java program is usually organized in sequential steps. These steps are the same and independent of the platform on which they are executed. The screenshot below shows how to compile and run Hello on the Linux operating system progressively:

You’ll also like:

  1. Java Applet Hello World Examples
  2. Java Compiler
  3. Java Tokens – What is Java Tokens?
  4. Java Program Structure
  5. Python First Hello World Program
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.