0% found this document useful (0 votes)
29 views3 pages

Notes 3-Gfg (Writing The First Program)

Uploaded by

abhisi690012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Notes 3-Gfg (Writing The First Program)

Uploaded by

abhisi690012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorials

DSA A
Data Science
Web Tech
Courses Writing First Program in Java

First Program - "Hello World"

The below-given program is the simplest program of Java, printing "Hello World" to
the screen. Let us try to understand every bit of the code step by step.

Java

// This is a simple Java program.


class HelloWorld
{
// Your program begins with a call to main().
// Prints "Hello, World" to the terminal window.
public static void main(String args[])
{
System.out.println("Hello, World");
}
}

Output:

Hello, World

The "Hello World!" program consists of three primary components: the HelloWorld
class definition, the main method, and the source code comments. The following
explanation will provide you with a basic understanding of the code:

1. Class definition: Below line uses the keyword class to declare that a new class
is being defined.

class HelloWorld
Articles Read  Prev
0 of 3 Complete. (0%)
Next 
Dash
1. HelloWorld is an identifier that is the name of the class. The entire class
definition, including all of its members, will be between the opening curly
brace { and the closing curly brace }.
All
2. main method: In Java programming language, every application must contain
a main method whose signature is:

public static void main(String[] Articles


args)
public: So that JVM can execute the method from anywhere.
static: Main method is to be called without object.
The modifiers public and static can
Videosbe written in either order.
void: The main method doesn't return anything.
main(): Name configured in the JVM.
String[]: The main method accepts a single argument:
Problems
an array of elements of type String.

1. Like in C/C++, the main method is the entry point for your application and will
Quiz
subsequently invoke all the other methods required by your program.
2. The next line of code is shown here. Notice that it occurs inside the main( ).

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

1. This line outputs the string "Hello, World" followed by a new line on the screen.
Output is actually accomplished by the built-in println( ) method. System is a
predefined class that provides access to the system, and out is the variable of
type output stream that is connected to the console.
2. Comments: They can either be multi-line or single-line comments.

/* This is a simple Java program.


Call this file "HelloWorld.java". */

1. This is a multiline comment. This type of comment must begin with /* and end
with */. For single-line you may directly use // as in C/C++.

Important Points :

The name of the class defined by the program is HelloWorld, which is the
same as the name of the file(HelloWorld.java). This is not a coincidence. In
Java, all codes must reside inside a class and there is at most one public class
which contains the main() method.
By convention, the name of the main class(a class that contains the main
Menu
method) should match the name of the file that holds the program.

Articles Read 
0 of 3 Complete. (0%)

Dash

All

Mark as Read

Articles

 Report An Issue

Videos
If you are facing any issue on this page. Please let us know.

Problems

Quiz

Menu

Articles Read 
0 of 3 Complete. (0%)

You might also like