Program की Structure
एक basic Scala program एक object के अंदर रहता है जिसमें main नाम की एक शुरुआती जगह होती है। यह उस stage जैसा है जिसके दरवाज़े पर लिखा हो 'यहां से शो शुरू होता है'।
In this page:
Syntax
object ObjectName {
def main(args: Array[String]): Unit = {
// program code
}
}
Program Structure
एक basic Scala program एक object के रूप में व्यवस्थित होता है जिसमें एक main method होता है (या App को extend करता है), और statements क्रम में execute होते हैं।
object Main extends App शॉर्टहैंड आपको explicit main method में लपेटने के बजाय top-level statements सीधे object के body में लिखने देता है। दोनों styles एक चलने योग्य program में compile होती हैं।
Note:
scripts और छोटे programs के लिए
extends App सुविधाजनक है, लेकिन बड़े, production Scala code में explicit def main(args: Array[String]): Unit ज़्यादा आम है।उदाहरण: Program Structure
object Main {
def main(args: Array[String]): Unit = {
println("Program started")
greet()
println("Program finished")
}
def greet(): Unit = {
println("Hello from a helper method!")
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
objectके बजायclassके अंदरdef mainलिखना, जिससे Scala को static entry point नहीं मिलता।def main(args: Array[String])को बिनाUnitreturn type या गलत signature के साथ लिखना, जिससे program entry point के रूप में नहीं चलता।greet()को define होने से पहले call करना और यह मान लेना कि order मायने रखता है, जबकि किसी object के अंदर methods किसी भी order में define हो सकते हैं।
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: