← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 4 of 12

Java का पहला Program

आपका पहला Java program पहली बार hello कहने जैसा है। आप main नाम के एक starting spot वाली एक छोटी class बनाते हैं और इसे screen पर एक message print करने को कहते हैं।
Syntax
java
public class ClassName {
    public static void main(String[] args) {
        // statements
    }
}

The Main Class

Java में कोई free-floating functions नहीं -- हर method, main सहित, एक class के अंदर रहना चाहिए, और convention से file की public class को बिल्कुल इसका नाम share करना चाहिए ताकि compiler आपका entry point ढूंढ सके।

उदाहरण: The Main Class

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("main() lives inside a class - Java has no free-floating functions.");
	}
}

The main Method

JVM specifically public static void main(String[] args) ढूंढता है -- इस signature को गलत करें (गलत capitalization, missing static, अलग parameter type) और Java इसे program के starting point के रूप में नहीं पहचानेगी।

उदाहरण: The main Method

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("JVM looks for exactly this signature to start the program.");
	}
}

Printing Output

System.out.println() इसके argument को text में convert करता है और इसे standard output पर लिखता है, फिर एक newline character append करता है ताकि अगली println() call अपने आप एक नई line पर शुरू हो।

उदाहरण: Printing Output

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("First line");
		// Print a line to the console
		System.out.println("Second line automatically starts fresh");
	}
}

System and Out Classes

System java.lang में एक built-in class है, और out इसके static fields में से एक है -- एक PrintStream object जो आपके terminal से जुड़ा है -- यही कारण है कि आप हमेशा System.out लिखते हैं अपना खुद का instance बनाने के बजाय।

उदाहरण: System and Out Classes

java
public class Main {
	public static void main(String[] args) {
		java.io.PrintStream out = System.out; // out is a static PrintStream field of the System class
		out.println("Written through System's static 'out' field.");
	}
}

Statement Terminators

Python के विपरीत, Java को हर statement के आखिर में एक semicolon चाहिए यह मार्क करने के लिए कि यह कहां खत्म होता है; compiler आपका code सही से parse करने के लिए इसे उपयोग करता है, और एक छोड़ना beginners को मिलने वाले सबसे आम पहले syntax errors में से एक है।

उदाहरण: Statement Terminators

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare a and set it to 5
		int a = 5;
		// Declare b and set it to 10
		int b = 10;
		// Print a line to the console
		System.out.println(a + b);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. File को public class से अलग नाम देना, इसलिए Main.java में public class Hello compile होने में fail होता है।
  2. Main method signature गलत लिखना, जैसे public void main(String[] args), इसलिए JVM entry point नहीं ढूंढ पाता।
  3. system.out.println lowercase s के साथ लिखना, जो एक compile error है क्योंकि class System है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.