Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C37-L1349-E5977184

Home Dashboard
Chapter 3 · Module 1 — Java Fundamentals

Java Program Structure and First Program

4,951
words
28
min read
3
tools

1. Learning Outcomes#

इस chapter को पूरा करने के बाद आप:

  • Java source file की basic structure समझ पाएँगे
  • .java file extension का purpose समझ पाएँगे
  • traditional class-based Java program लिख पाएँगे
  • class declaration और { } braces की basic भूमिका समझ पाएँगे
  • public static void main(String[] args) को beginner level पर समझ पाएँगे
  • String[] args का high-level purpose समझ पाएँगे
  • Java statements और semicolon ; का उपयोग समझ पाएँगे
  • System.out.println() से console पर output print कर पाएँगे
  • basic Hello World program खुद लिख पाएँगे
  • public class और source file name का relationship समझ पाएँगे
  • javac से Java program compile और java से run कर पाएँगे
  • compilation error और program launch error के बीच अंतर समझ पाएँगे
  • java FileName.java से source file directly run करना समझ पाएँगे
  • Java 25+ में instance main methods और compact source files का basic use समझ पाएँगे
  • traditional और modern compact Java program structure में सही चुनाव कर पाएँगे

2. Java Program आखिर लिखा कहाँ जाता है?#

Java program लिखते समय हम सबसे पहले एक source file बनाते हैं।

Source file वह file होती है जिसमें हम Java language में अपना code लिखते हैं।

Java source file का extension होता है:

.java

उदाहरण:

  • HelloWorld.java
  • Student.java
  • Calculator.java
  • Employee.java

अगर आपने HelloWorld.java नाम की file बनाई, तो उस file के अंदर Java source code होगा।

Remember

.java source code की file होती है। Compile होने के बाद सामान्यतः Java compiler .class bytecode file बनाता है।

शुरुआत में Java program को इस mental model से समझें:

Visual Diagram80%

Rendering diagram…

अभी हमें बस इतना समझना है:

File के अंदर class → class के अंदर main method → main के अंदर statements.

यही traditional Java program की basic structure है।

3. Traditional Class-Based Java Program#

एक basic traditional Java program देखें:

JAVA
public class HelloWorld {

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

इस छोटे program में कई शब्द दिखाई दे रहे हैं।

पहली बार देखने पर यह थोड़ा बड़ा लग सकता है।

लेकिन इसे एक साथ समझने की कोशिश नहीं करेंगे।

हम बाहर से अंदर की तरफ धीरे-धीरे समझेंगे।

Figure 1: Traditional Java program structure from source file to statement
Traditional Java program structure from source file to statementSource File: HelloWorld.javaSource File: HelloWorld.javaClass Declaration: public class HelloWorldClass Declaration: public class HelloWorldEntry Point: public static void main(String[] args)Entry Point: public static void main(String[] args)Statement: System.out.println("Hello, World!");Statement: System.out.println("Hello, World!");

इस visual में सबसे महत्वपूर्ण बात hierarchy है।

Source file के अंदर class है।

Class के अंदर main() method है।

और main() method के अंदर actual executable statements हैं।

4. Class Declaration समझें#

हमारे program की पहली महत्वपूर्ण line है:

JAVA
public class HelloWorld {

अभी class को बहुत गहराई से समझने की जरूरत नहीं है।

इस chapter के लिए class को program के एक container की तरह समझें।

हमने class का नाम रखा है:

HelloWorld

इसलिए declaration है:

JAVA
public class HelloWorld {

यहाँ:

  • public एक access-related keyword है
  • class Java को बताता है कि हम class declare कर रहे हैं
  • HelloWorld हमारी class का नाम है
  • { class body की शुरुआत बताता है

Class body बाद में } से बंद होती है।

JAVA
public class HelloWorld {

}

इसे एक box की तरह सोच सकते हैं।

Opening brace { मतलब box शुरू।

Closing brace } मतलब box समाप्त।

Key Idea

Braces { } Java code के अलग-अलग blocks की boundaries दिखाती हैं।

5. Opening और Closing Braces को सही समझें#

हमारे complete program को indentation के साथ देखें:

JAVA
public class HelloWorld {

    public static void main(String[] args) {

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

    }

}

यहाँ braces के दो pairs हैं।

पहला pair:

JAVA
public class HelloWorld {
    ...
}

यह class को खोलता और बंद करता है।

दूसरा pair:

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

यह main() method को खोलता और बंद करता है।

इसलिए structure logically ऐसा है:

Visual Diagram80%

Rendering diagram…

Common Mistake

Beginner अक्सर एक closing brace } भूल जाते हैं या extra brace लिख देते हैं। इससे compilation error हो सकता है।

Indentation Java syntax का हर जगह mandatory हिस्सा नहीं है, लेकिन readable code के लिए indentation बहुत महत्वपूर्ण है।

इसलिए nested code को अंदर की तरफ लिखना अच्छी habit है।

6. Program Entry Point क्या है?#

जब हम Java application launch करते हैं, Java को यह पता होना चाहिए कि program execution कहाँ से शुरू करनी है।

Traditional Java program में हम सामान्यतः यह method लिखते हैं:

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

इसे program का entry point समझ सकते हैं।

Traditional form में Java program की execution यहीं से शुरू होती है।

पूरी line को एक साथ याद करने के बजाय उसके parts समझें।

public#

Traditional declaration में main() को public लिखा जाता है।

Beginner level पर इतना याद रखें कि traditional main signature में यह standard हिस्सा है।

static#

Traditional main() method को static लिखा जाता है।

static का पूरा meaning classes और objects समझने के बाद ज्यादा स्पष्ट होगा।

अभी इतना समझें कि traditional launcher style में Java इस method को program शुरू करने के लिए use कर सकता है बिना आपके द्वारा पहले कोई object manually बनाने की आवश्यकता के।

void#

void बताता है कि यह method कोई value return नहीं करता।

main#

main method का विशेष नाम है जिसे Java launcher program के entry point के रूप में पहचान सकता है।

Case महत्वपूर्ण है।

यह:

main

सही है।

यह:

Main

अलग नाम है।

String[] args#

Traditional main method में:

JAVA
String[] args

command-line arguments प्राप्त करने के लिए parameter है।

इसे अभी बहुत गहराई से समझने की जरूरत नहीं है।

मान लीजिए program को command line से अतिरिक्त information दी गई:

BASH
java HelloWorld Rahul

तो "Rahul" जैसी command-line value args के माध्यम से program को मिल सकती है।

String[] का detailed array behavior आगे array सीखते समय ज्यादा स्पष्ट होगा।

Remember

अभी String[] args को बस इतना समझें: command line से आने वाली text values को program तक पहुँचाने का traditional तरीका।

Parameter का variable name हमेशा args होना आवश्यक नहीं है।

उदाहरण के लिए यह भी valid traditional signature हो सकती है:

JAVA
public static void main(String[] values) {
}

लेकिन args सबसे common convention है।

7. Traditional Main Method को एक साथ पढ़ें#

अब इस line को फिर देखें:

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

इसे beginner-friendly तरीके से ऐसे पढ़ सकते हैं:

यह वह traditional main method है जहाँ से हमारा Java program शुरू होगा।

हर keyword का deep technical meaning अभी याद करना जरूरी नहीं है।

पहले structure पहचानना जरूरी है।

8. Statements कहाँ लिखे जाते हैं?#

अब हमारे पास class और main() method है।

लेकिन program वास्तव में करेगा क्या?

उसके लिए हम main() के अंदर statements लिखते हैं।

उदाहरण:

JAVA
public class HelloWorld {

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

यहाँ:

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

एक statement है।

Statement को फिलहाल program को दिया गया एक instruction समझें।

यह instruction कह रहा है:

Console पर Hello, World! print करो।

9. Semicolon ; क्यों लगाया जाता है?#

Statement के अंत में देखें:

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

आखिर में:

;

है।

Java में बहुत से statements semicolon ; से समाप्त होते हैं।

यह compiler को statement की boundary समझने में मदद करता है।

उदाहरण:

JAVA
System.out.println("Java");
System.out.println("SQL");
System.out.println("Python");

तीनों statements अलग-अलग semicolon से समाप्त हुए हैं।

Common Mistake

System.out.println("Hello") के बाद semicolon भूलना beginner compilation errors में से एक common mistake है।

गलत:

JAVA
System.out.println("Hello")

सही:

JAVA
System.out.println("Hello");

लेकिन हर closing brace के बाद semicolon लगाने का rule नहीं है।

उदाहरण:

JAVA
public class Demo {
}

यहाँ class की closing brace के बाद semicolon आवश्यक नहीं है।

10. System.out.println() क्या करता है?#

अब हमारे first program का actual काम समझते हैं।

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

इसका काम console पर text print करना है।

फिलहाल इसे तीन छोटे parts में समझें:

  • System — Java की उपलब्ध system-related functionality का हिस्सा
  • out — standard output से जुड़ा हिस्सा
  • println() — value print करके अगली line पर जाने वाला operation

इनके object-level internals अभी समझना जरूरी नहीं है।

हमारा current goal सिर्फ output print करना सीखना है।

सबसे छोटा example#

JAVA
public class Demo {

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

Expected Output#

TEXT
Hello

Output में Hello इसलिए आया क्योंकि println() के parentheses के अंदर "Hello" दिया गया था।

Double quotes के अंदर दिया गया text console पर print हुआ।

11. Multiple Lines Print करना#

अब दो statements लिखते हैं:

JAVA
public class Demo {

    public static void main(String[] args) {
        System.out.println("Java");
        System.out.println("Learning Started");
    }
}

Expected Output#

TEXT
Java
Learning Started

पहला println() पहली line print करता है।

दूसरा println() अगली line print करता है।

यही कारण है कि दोनों outputs अलग-अलग lines पर दिखाई देते हैं।

12. हमारा पहला Complete Hello World Program#

अब पूरा program फिर से देखें:

JAVA
public class HelloWorld {

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

Expected Output#

TEXT
Hello, World!

अब program को mentally trace करें:

  1. Java source file का नाम HelloWorld.java है।
  2. File में HelloWorld class declare की गई है।
  3. Class के अंदर main() method है।
  4. main() के अंदर System.out.println() statement है।
  5. Program launch होने पर main method execute होती है।
  6. Statement console पर Hello, World! print करता है।

बस यही हमारा पहला complete Java program है।

13. Source File Name और Public Class Name का Relationship#

इस rule को शुरुआत से सही समझना बहुत जरूरी है।

अगर आपने public top-level class लिखी है:

JAVA
public class HelloWorld {
}

तो source file का नाम होना चाहिए:

HelloWorld.java

अगर class है:

JAVA
public class Student {
}

तो file का नाम होना चाहिए:

Student.java

अगर class है:

JAVA
public class Calculator {
}

तो file का नाम होना चाहिए:

Calculator.java

गलत example#

मान लीजिए file का नाम है:

HelloWorld.java

लेकिन code है:

JAVA
public class Welcome {

    public static void main(String[] args) {
        System.out.println("Welcome");
    }
}

यहाँ public class का नाम Welcome है लेकिन file का नाम HelloWorld.java है।

Compiler इसे स्वीकार नहीं करेगा।

सही विकल्प होगा:

  • file को Welcome.java नाम दें

या

  • public class का नाम HelloWorld करें
महत्त्वाचे

Public top-level class का नाम और .java source file का नाम match होना चाहिए।

अगर top-level class public नहीं है तो language rules अलग flexibility देते हैं, लेकिन beginner और professional code organization के लिए class और file का नाम match रखना एक अच्छी सामान्य practice है।

14. Java Program Compile क्यों किया जाता है?#

हमने source code लिखा:

HelloWorld.java

Computer सीधे इसी Java source text को वैसे execute नहीं करता जैसे हम उसे पढ़ते हैं।

Java development flow में compiler source code को process करता है।

Traditional command-line flow में हम javac compiler का उपयोग करते हैं।

Command:

BASH
javac HelloWorld.java

अगर source code valid है, compiler bytecode वाली .class file तैयार करता है।

इस example में सामान्यतः:

HelloWorld.class

मिलेगी।

फिर compiled program launch करने के लिए:

BASH
java HelloWorld

लिखते हैं।

यहाँ ध्यान दें:

हम लिखते हैं:

java HelloWorld

न कि:

java HelloWorld.class

Traditional compile-and-run flow को visual रूप में देखें:

Visual Diagram80%

Rendering diagram…

इस flow में:

javac

का मुख्य काम source code compile करना है।

java

का मुख्य काम Java program launch करना है।

15. Step-by-Step Compile और Run#

मान लीजिए HelloWorld.java में यह code है:

JAVA
public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("My first Java program");
    }
}

Step 1 — File Save करें#

File:

HelloWorld.java

Step 2 — Compile करें#

BASH
javac HelloWorld.java

Successful compilation पर terminal में जरूरी नहीं कि कोई success message दिखाई दे।

अगर compilation successful हुई, compiled class file बन सकती है:

HelloWorld.class

Step 3 — Program Run करें#

BASH
java HelloWorld

Expected Output#

TEXT
My first Java program

अब source file से execution तक पूरा basic lifecycle आपके सामने है।

16. Compilation Error क्या है?#

Compilation error तब होता है जब compiler source code को valid Java program के रूप में compile नहीं कर पाता।

उदाहरण:

JAVA
public class Demo {

    public static void main(String[] args) {
        System.out.println("Hello")
    }
}

यहाँ println() statement के बाद semicolon missing है।

इसलिए:

BASH
javac Demo.java

चलाने पर compilation fail होगी।

Program successfully compiled form तक नहीं पहुँचेगा।

दूसरे common beginner compilation issues:

  • semicolon missing
  • brace missing
  • incorrect syntax
  • public class और filename mismatch

17. Program Launch Error क्या है?#

अब अलग situation देखें।

मान लीजिए code successfully compile हो चुका है।

लेकिन आप गलत class launch कर रहे हैं:

BASH
java WrongName

जब ऐसी class उपलब्ध ही नहीं है, Java launcher program start नहीं कर पाएगा।

यह source syntax compilation error नहीं है।

यह launch-stage problem है।

इसी तरह अगर launched class में suitable launchable main method नहीं मिलता, program launch नहीं हो सकता।

दोनों में अंतर#

Situation Problem कब आती है? Example
Compilation error javac source code compile करते समय Missing semicolon
Launch error java program start करते समय Wrong class name
Launchable main missing java entry point खोजते समय Suitable main method नहीं मिला
Key Idea

पहले पूछें: error compile करते समय आई या run/launch करते समय? इससे debugging की direction तुरंत clear हो जाती है।

18. Source File को सीधे Run करना#

Traditional workflow में हमने किया:

BASH
javac HelloWorld.java
java HelloWorld

लेकिन modern Java में single source-file program को सीधे launch भी किया जा सकता है।

उदाहरण:

BASH
java HelloWorld.java

अगर HelloWorld.java में यह code है:

JAVA
public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Direct source execution");
    }
}

तो:

BASH
java HelloWorld.java

source-file mode में program को compile और launch करने की आवश्यक processing automatically handle करता है।

आपको पहले अलग से javac command चलाना जरूरी नहीं है।

Expected Output#

TEXT
Direct source execution
Remember

java HelloWorld.java और java HelloWorld को confuse न करें।

पहला source file directly launch करता है।

दूसरा compiled class को class name से launch करता है।

19. Direct Source Execution और Compact Source File एक ही चीज नहीं हैं#

यह एक महत्वपूर्ण beginner confusion है।

इन दोनों concepts को अलग रखें।

Direct source execution#

यह बताता है कि program run कैसे किया गया।

उदाहरण:

BASH
java HelloWorld.java

Traditional class-based source file भी इस तरह run किया जा सकता है।

Compact source file#

यह बताता है कि source code लिखा कैसे गया है।

Java 25+ में simple program लिखते समय explicit class declaration omit की जा सकती है।

इसलिए:

execution style और source structure दो अलग concepts हैं।

20. Java 25+ में Main Method का Modern Form#

Traditional Java सीखते समय आपने यह देखा:

JAVA
public static void main(String[] args)

यह form आज भी valid और बहुत important है।

लेकिन Java 25 में program launch protocol को ज्यादा flexible बनाया गया।

main method अब हर situation में public static और String[] args वाला traditional form ही हो, यह आवश्यक नहीं है।

उदाहरण:

JAVA
class HelloWorld {

    void main() {
        System.out.println("Hello from instance main");
    }
}

यहाँ:

JAVA
void main()

एक instance main method है।

यह static नहीं है।

इसमें String[] args भी नहीं है।

Java 25+ launch rules suitable instance main method को program entry point के रूप में use कर सकते हैं।

Expected Output#

TEXT
Hello from instance main

इसका मतलब traditional main गलत या obsolete नहीं हुआ।

बल्कि Java ने simple programs के लिए additional launch forms provide किए हैं।

21. Compact Source Files — Java 25+#

अब एक और बड़ा simplification देखें।

Traditional program:

JAVA
public class HelloWorld {

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

Java 25+ compact source file में simple program ऐसा लिखा जा सकता है:

JAVA
void main() {
    System.out.println("Hello, World!");
}

ध्यान दें:

  • explicit class HelloWorld declaration नहीं है
  • public नहीं है
  • static नहीं है
  • String[] args नहीं है
  • फिर भी हमारे पास launchable main method है

यह कोई अलग scripting language नहीं है।

यह Java ही है।

Compiler background में इस compact source file के top-level members को एक implicitly declared class के members के रूप में treat करता है।

Beginner के लिए simple mental model:

Visual Diagram80%

Rendering diagram…

इसका purpose simple Java programs के लिए शुरुआती boilerplate कम करना है।

22. Compact Hello World को Run करना#

File बनाएँ:

HelloWorld.java

Code:

JAVA
void main() {
    System.out.println("Hello from compact Java");
}

इसे direct source-file mode में चला सकते हैं:

BASH
java HelloWorld.java

Expected Output#

TEXT
Hello from compact Java

Java 25+ में compact source files और instance main methods permanent language functionality हैं; explicit class declaration के बिना simple source file और parameterless instance void main() जैसे forms supported हैं.

23. Compact Program को Explicitly Compile करना#

Compact source file को केवल direct mode में ही चलाना जरूरी नहीं है।

इसे javac से compile भी किया जा सकता है।

मान लीजिए:

HelloWorld.java

में:

JAVA
void main() {
    System.out.println("Compact program");
}

Compile करें:

BASH
javac HelloWorld.java

फिर launch करें:

BASH
java HelloWorld

Java 25 documentation के अनुसार compact source file को direct source-file mode या explicit compile-and-launch flow दोनों से run किया जा सकता है.

24. Traditional और Compact Structure की तुलना#

Point Traditional Class-Based Program Java 25+ Compact Source File
Explicit class declaration Yes नहीं
Common entry point public static void main(String[] args) void main() संभव
static आवश्यक? Traditional signature में yes Instance main में नहीं
String[] args आवश्यक? Traditional signature में yes Parameterless main संभव
Best suited for Conventional applications, existing codebases, structured learning Small programs, learning, quick single-file programs
Java 25+ में valid? Yes Yes
Remember

Compact source file traditional Java को replace नहीं करती। यह simple programs के लिए एक additional simpler form है।

25. Job-Ready Learner को कौन-सा Structure सीखना चाहिए?#

दोनों समझना useful है।

लेकिन job-oriented Java learner के लिए traditional structure बहुत महत्वपूर्ण है:

JAVA
public class Application {

    public static void main(String[] args) {
        System.out.println("Application started");
    }
}

क्योंकि:

  • existing Java projects में explicit classes बहुत common हैं
  • frameworks और enterprise applications structured classes use करते हैं
  • interviews में traditional main() signature आज भी commonly पूछा जाता है
  • older Java versions और बहुत से existing environments traditional structure use करते हैं

साथ ही Java 25+ compact syntax को पहचानना भी जरूरी है ताकि modern Java code देखकर confusion न हो।

सही approach है:

Traditional structure अच्छी तरह सीखें + modern compact structure को भी समझें।

26. तीन Structures को एक साथ देखें#

Traditional compile-and-run#

Code:

JAVA
public class Demo {

    public static void main(String[] args) {
        System.out.println("Traditional");
    }
}

Commands:

BASH
javac Demo.java
java Demo

Traditional source file direct run#

वही code:

JAVA
public class Demo {

    public static void main(String[] args) {
        System.out.println("Direct");
    }
}

Command:

BASH
java Demo.java

Java 25+ compact source file#

JAVA
void main() {
    System.out.println("Compact");
}

Command:

BASH
java Demo.java

यही तीन forms अलग-अलग identify कर पाना इस chapter का important practical outcome है।

27. Common Beginner Mistakes#

Class name और file name mismatch#

गलत combination:

File:

Demo.java

Code:

JAVA
public class Test {
}

Public class और filename match करें।

Semicolon भूल जाना#

गलत:

JAVA
System.out.println("Hello")

सही:

JAVA
System.out.println("Hello");

main को Main लिखना#

गलत traditional entry method:

JAVA
public static void Main(String[] args) {
}

Java identifiers case-sensitive हैं।

Main और main अलग names हैं।

Run command में .class लगाना#

सामान्य compiled-class launch:

BASH
java Demo

यह नहीं:

BASH
java Demo.class

Compile command और run command confuse करना#

Compile:

BASH
javac Demo.java

Run compiled class:

BASH
java Demo

Directly run source file:

BASH
java Demo.java

तीनों commands का purpose अलग है।

Compact source file को पुराने JDK पर चलाने की कोशिश करना#

Java 25 compact source file syntax को use करने के लिए compatible modern JDK चाहिए।

अगर environment पुराना है तो:

JAVA
void main() {
    System.out.println("Hello");
}

वाला compact form supported नहीं होगा।

28. Hands-On Practice#

Practice 1#

Requirement

Traditional class-based Java program बनाइए जो console पर Welcome to Java print करे।

Your Task

Welcome.java file बनाकर सही public class, traditional main method और System.out.println() statement लिखें।

HintShow Hint

Public class का नाम file name से match होना चाहिए।

SolutionShow Solution
JAVA
public class Welcome {

    public static void main(String[] args) {
        System.out.println("Welcome to Java");
    }
}

Explanation

File का नाम Welcome.java होगा।

Welcome class के अंदर traditional main() method program का entry point है।

println() required message console पर print करता है।


Practice 2#

Requirement

नीचे दिए program की mistake पहचानें और उसे fix करें।

JAVA
public class Message {

    public static void main(String[] args) {
        System.out.println("Java is running")
    }
}

Your Task

Program को compilable बनाइए।

HintShow Hint

Print statement के end को ध्यान से देखें।

SolutionShow Solution
JAVA
public class Message {

    public static void main(String[] args) {
        System.out.println("Java is running");
    }
}

Explanation

Original statement के अंत में semicolon ; missing था।

Java compiler statement को सही तरह terminate नहीं कर पा रहा था।


Practice 3#

Requirement

Java 25+ compact source file बनाइए जो Modern Java print करे।

Your Task

Explicit class declaration के बिना parameterless instance main() use करें।

HintShow Hint

इस form में शुरुआत सीधे void main() से की जा सकती है।

SolutionShow Solution
JAVA
void main() {
    System.out.println("Modern Java");
}

Explanation

यह Java 25+ compact source file है।

Explicit class declaration source code में नहीं लिखी गई है।

void main() launchable instance main method के रूप में use हो सकती है।

इसे source-file mode में java FileName.java से launch किया जा सकता है।

29. Self-Check#

javac HelloWorld.java और java HelloWorld में मुख्य अंतर क्या है?Show Answer

javac HelloWorld.java source code को compile करता है।

java HelloWorld compiled class को launch करता है।


क्या Java 25+ compact source file का मतलब Java में classes खत्म हो गईं?Show Answer

नहीं।

Compact source file में explicit class declaration source code से omit की जा सकती है, लेकिन compiler program को internally implicitly declared class के context में handle करता है।

इसलिए compact source file Java के class model को खत्म नहीं करती।

30. Interview Preparation#

What is the traditional entry point of a Java application?Show Answer

The traditional entry point of a Java application is:

public static void main(String[] args)

The Java launcher can invoke this method to start program execution.

Key Points:

  • main is the conventional entry method name.
  • void means the method does not return a value.
  • String[] args receives command-line arguments.
  • The traditional form uses public and static.

Why interviewer asks this:

The interviewer wants to verify whether the candidate understands the basic execution structure of a Java application.


What is the difference between javac and java commands?Show Answer

javac is the Java compiler. It compiles Java source files into bytecode class files.

java is the Java launcher. It launches a Java application or, in source-file mode, can launch a supported source-code program directly.

Key Points:

  • javac Demo.java compiles the source file.
  • java Demo launches the compiled class.
  • java Demo.java uses source-file launch mode.

Why interviewer asks this:

The interviewer checks whether the candidate understands the basic Java build and execution lifecycle.


What is the relationship between a public class name and its Java source file name?Show Answer

A public top-level class must be declared in a source file whose base name matches the public class name.

For example, public class Employee should be declared in Employee.java.

Key Points:

  • Public top-level class name and source filename must match.
  • The .java extension identifies the Java source file.
  • A mismatch causes a compilation problem.

Why interviewer asks this:

This checks the candidate's understanding of basic Java compilation-unit rules.


What changed about main methods and simple source files in Java 25?Show Answer

Java 25 permanently supports compact source files and a more flexible launch protocol for main methods.

A launchable main method can be an instance method and can have no parameters. Compact source files can also omit an explicit class declaration for simple programs.

Key Points:

  • main does not always have to be static in Java 25+.
  • A parameterless void main() can be launchable.
  • Compact source files can omit an explicit class declaration.
  • The traditional public static void main(String[] args) form remains valid.

Why interviewer asks this:

The interviewer wants to know whether the candidate understands both traditional Java structure and modern Java language evolution.

31. Chapter Recap#

इस chapter से मुख्य बातें याद रखें:

  • Java source file का extension .java है।
  • Traditional Java program में code सामान्यतः class के अंदर लिखा जाता है।
  • { और } code blocks की boundaries दिखाती हैं।
  • Traditional entry point public static void main(String[] args) है।
  • String[] args command-line text arguments प्राप्त कर सकता है।
  • Program के executable statements main() के अंदर लिखे जा सकते हैं।
  • बहुत से Java statements semicolon ; से समाप्त होते हैं।
  • System.out.println() console पर output print करता है।
  • Public top-level class और source file का नाम match होना चाहिए।
  • javac FileName.java source file compile करता है।
  • java ClassName compiled class launch करता है।
  • java FileName.java source-file mode में source program directly launch कर सकता है।
  • Compilation error और launch error अलग stages पर होते हैं।
  • Java 25+ instance main methods allow करता है।
  • Java 25+ compact source files explicit class declaration के बिना simple programs लिखने देती हैं।
  • Traditional structure आज भी पूरी तरह valid और job-oriented Java learning के लिए महत्वपूर्ण है।
  • Modern compact structure को traditional structure का replacement नहीं, बल्कि additional simpler option समझें।
Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.