Java का पहला Program
In this page:
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
// 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.");
}
}
Login to try C/C++/Java/PHP code in the editor
The main Method
JVM specifically public static void main(String[] args) ढूंढता है -- इस signature को गलत करें (गलत capitalization, missing static, अलग parameter type) और Java इसे program के starting point के रूप में नहीं पहचानेगी।
उदाहरण: The main Method
// 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.");
}
}
Login to try C/C++/Java/PHP code in the editor
Printing Output
System.out.println() इसके argument को text में convert करता है और इसे standard output पर लिखता है, फिर एक newline character append करता है ताकि अगली println() call अपने आप एक नई line पर शुरू हो।
उदाहरण: Printing Output
// 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");
}
}
Login to try C/C++/Java/PHP code in the editor
System and Out Classes
System java.lang में एक built-in class है, और out इसके static fields में से एक है -- एक PrintStream object जो आपके terminal से जुड़ा है -- यही कारण है कि आप हमेशा System.out लिखते हैं अपना खुद का instance बनाने के बजाय।
उदाहरण: System and Out Classes
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.");
}
}
Login to try C/C++/Java/PHP code in the editor
Statement Terminators
Python के विपरीत, Java को हर statement के आखिर में एक semicolon चाहिए यह मार्क करने के लिए कि यह कहां खत्म होता है; compiler आपका code सही से parse करने के लिए इसे उपयोग करता है, और एक छोड़ना beginners को मिलने वाले सबसे आम पहले syntax errors में से एक है।
उदाहरण: Statement Terminators
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- File को public class से अलग नाम देना, इसलिए
Main.javaमेंpublic class Hellocompile होने में fail होता है। - Main method signature गलत लिखना, जैसे
public void main(String[] args), इसलिए JVM entry point नहीं ढूंढ पाता। system.out.printlnlowercasesके साथ लिखना, जो एक compile error है क्योंकि classSystemहै।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: