0% found this document useful (0 votes)
3 views2 pages

Java Program Structure and Examples

The document provides an overview of a basic Java program structure, including the HelloWorld class and its main method. It explains the significance of each component, such as class accessibility, command-line arguments, and output functions. Additionally, it covers how to write comments in Java and includes a complete example program that prints student information.
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)
3 views2 pages

Java Program Structure and Examples

The document provides an overview of a basic Java program structure, including the HelloWorld class and its main method. It explains the significance of each component, such as class accessibility, command-line arguments, and output functions. Additionally, it covers how to write comments in Java and includes a complete example program that prints student information.
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

Java Program

1. Structure of a Basic Java Program


public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

2. Detailed Explanation of Each Part

public class HelloWorld

• In Java, the program must be inside a class. Here, HelloWorld is the


name of the class.
• The public keyword indicates that the class is accessible from
anywhere.

public static void main(String[] args)

• main() is the entry point of the Java program, like in C.


• String[] args is used to accept command-line arguments.
• public allows access from other classes.
• static means this method can be run without creating an instance of the
class.
• void means that this method does not return any value.

[Link]("Hello, World!");

• This is the output function in Java, similar to printf() in C. It prints the


given text to the console.

3. Comments — Writing Notes in Code

In Java, comments are written similarly to C.

Single-Line Comment:
// This is a single-line comment

Multi-Line Comment:
/* This is a
multi-line comment */
4. Complete Example Program
public class StudentInfo {
public static void main(String[] args) {
// The following lines print name and age
[Link]("Name: Nafis");
[Link]("Age: 16");
}
}

Output:
Name: Nafis
Age: 16

You might also like