Introduction
Java program एक fixed structure follow करता है। Java में हर program किसी न किसी class के अंदर लिखा जाता है। Java program की execution हमेशा main() method से start होती है।
Java program structure को समझना बहुत जरूरी है क्योंकि इसी से हमें पता चलता है कि class कहाँ लिखनी है, method कहाँ लिखना है, statements कहाँ लिखने हैं और output कैसे print करना है।
Simple Definition
Java program structure is the standard format used to write a Java program with class, main method, and statements.
Hindi Meaning
Java program structure वह fixed format है जिसके अनुसार Java program लिखा जाता है। इसमें class, main method और program statements शामिल होते हैं।
Basic Structure of Java Program
class ClassName {
public static void main(String[] args) {
// statements
}
}
Explanation
इस structure में class keyword से class बनाई जाती है। Class के अंदर main() method लिखा जाता है। Main method के अंदर program के statements लिखे जाते हैं।
Parts of Java Program Structure
Documentation Section
Documentation section में program से related comments लिखे जाते हैं। Comments program को समझाने के लिए use होते हैं। Compiler comments को execute नहीं करता।
// This is a simple Java program
या
/*
This program prints a message.
*/
Package Statement
Package statement का use classes को group में organize करने के लिए किया जाता है। Package statement program की first line में लिखा जाता है।
package mypackage;
Simple BCA level programs में package statement usually use नहीं किया जाता।
Import Statement
Import statement का use Java की predefined classes और packages को program में use करने के लिए किया जाता है।
import java.util.Scanner;
यह statement Scanner class को program में use करने की permission देता है।
Interface Section
Interface section में interface define किया जाता है। Interface में abstract methods होते हैं। यह part advanced programs में use होता है।
interface Printable {
void print();
}
Class Definition
Java में हर program class के अंदर लिखा जाता है। Class एक blueprint की तरह होती है जिसमें variables और methods लिखे जाते हैं।
class Hello {
// variables and methods
}
Main Method
Main method Java program का starting point होता है। जब Java program run होता है, तो JVM सबसे पहले main method को execute करता है।
public static void main(String[] args) {
// statements
}
Statements
Statements वह instructions होते हैं जिन्हें computer execute करता है। Java में हर statement के end में semicolon ; लगाना जरूरी होता है।
System.out.println("Hello Java");
Complete Java Program Structure Example
// Documentation Section
import java.util.Scanner;
class StudentDemo {
public static void main(String[] args) {
System.out.println("Welcome to Java Program Structure");
}
}
Output
Welcome to Java Program Structure
Explanation of Complete Program
Comment Line
// Documentation Section
यह comment है। इसका use program को explain करने के लिए किया जाता है।
Import Statement
import java.util.Scanner;
यह Java की Scanner class को import करता है। हालांकि इस program में Scanner का use नहीं किया गया है, लेकिन structure समझाने के लिए इसे दिखाया गया है।
Class Declaration
class StudentDemo
यह class declaration है। यहाँ class का नाम StudentDemo है।
Main Method
public static void main(String[] args)
यह Java program का main method है। Program की execution यहीं से start होती है।
Output Statement
System.out.println("Welcome to Java Program Structure");
यह statement screen पर message print करता है।
Simple Program Without Import Statement
class Hello {
public static void main(String[] args) {
System.out.println("Hello BCA Students");
}
}
Output
Hello BCA Students
Explanation
इस program में कोई import statement नहीं है क्योंकि program में कोई external class use नहीं की गई है। यह Java का सबसे simple structure है।
Program Using Scanner Class
import java.util.Scanner;
class InputDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);
}
}
Sample Output
Enter your name: Rahul
Hello Rahul
Explanation
इस program में Scanner class का use user से input लेने के लिए किया गया है। इसलिए program के starting में import java.util.Scanner; लिखा गया है।
Meaning of main Method Parts
public
public एक access modifier है। इसका मतलब है कि JVM main method को कहीं से भी access कर सकता है।
static
static का मतलब है कि main method को call करने के लिए object बनाने की जरूरत नहीं होती। JVM इसे directly call कर सकता है।
void
void का मतलब है कि main method कोई value return नहीं करता।
main
main method Java program का starting point होता है।
String[] args
String[] args command line arguments को store करता है। इसमें values string form में store होती हैं।
General Order of Java Program Structure
Java program में generally sections इस order में लिखे जाते हैं:
Documentation Section
Package Statement
Import Statements
Interface Section
Class Definition
Main Method
Statements
Simple programs में package और interface section जरूरी नहीं होते।
Important Rules of Java Program Structure
- Java program class के अंदर लिखा जाता है।
- Java program की execution
main()method से start होती है। - Class name meaningful होना चाहिए।
- Java case-sensitive language है।
- हर statement के end में semicolon
;लगाना जरूरी है। - Opening
{और closing}braces का सही use जरूरी है। - Public class का नाम और file name same होना चाहिए।
- Java source file का extension
.javaहोता है। - Compilation के बाद
.classfile बनती है।
Difference Between Source File and Class File
| Source File | Class File |
|---|---|
| Source file में Java code लिखा होता है। | Class file में bytecode होता है। |
इसका extension .java होता है। | इसका extension .class होता है। |
| इसे programmer लिखता है। | इसे compiler generate करता है। |
इसे javac command से compile किया जाता है। | इसे JVM execute करता है। |
Common Mistakes in Java Program Structure
main Method गलत लिखना
Wrong:
public void main(String[] args)
Correct:
public static void main(String[] args)
Semicolon भूल जाना
Wrong:
System.out.println("Hello")
Correct:
System.out.println("Hello");
Case-Sensitive Mistake
Wrong:
system.out.println("Hello");
Correct:
System.out.println("Hello");
Braces Mistake
Java में { } braces का सही use जरूरी है। अगर braces missing हैं, तो program compile नहीं होगा।
Exam-Oriented Short Answer
Q. Explain the structure of a Java program.
Java program एक fixed structure follow करता है। इसमें documentation section, package statement, import statement, class definition, main method और statements शामिल होते हैं। Java में हर program class के अंदर लिखा जाता है और program की execution public static void main(String[] args) method से start होती है। Main method के अंदर program statements लिखे जाते हैं। Output print करने के लिए System.out.println() statement का use किया जाता है।
Important Points to Remember
- Java program का basic structure class और main method पर based होता है।
- Documentation section comments के लिए use होता है।
- Import statement predefined classes को use करने के लिए लिखा जाता है।
- Main method Java program का entry point होता है।
- Statements main method के अंदर लिखे जाते हैं।
- Java में semicolon जरूरी होता है।
- Java case-sensitive language है।
- Public class का name और file name same होना चाहिए।
Practice Questions
Short Answer Questions
- Java program structure क्या होता है?
- Java program की execution कहाँ से start होती है?
- Documentation section का क्या use है?
- Import statement क्यों use किया जाता है?
- Main method का syntax लिखिए।
public static void main(String[] args)को explain कीजिए।- Source file और class file में अंतर लिखिए।
Long Answer Question
Java program की structure को example के साथ explain कीजिए।
Homework Program
एक Java program लिखिए जो user से name input ले और नीचे जैसा output print करे:
Enter your name: Amit
Welcome Amit to Java Programming
Hint
import java.util.Scanner;
class WelcomeUser {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Welcome " + name + " to Java Programming");
}
}