← Back to Kotlin Course | Chapter 1: Setup & Basics | Lesson 4 of 7

आपका पहला Kotlin Program

आपका पहला Kotlin program instructions का एक छोटा set है जो computer को screen पर एक message दिखाने के लिए कहता है।
Syntax
kotlin
fun main() {
    println("text")
}

The main() Function

हर चलने योग्य Kotlin program को main नाम का एक entry point function चाहिए, जो fun keyword से चिह्नित हो। जब आप compiled program चलाते हैं, तो execution main के curly braces के अंदर पहली line से शुरू होता है।

उदाहरण: The main() Function

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Hello from main()" to the console, with a trailing newline
    println("Hello from main()")
}

Printing Hello, World

किसी भी language में classic पहला program screen पर एक greeting print करता है। Kotlin में, println("Hello, World!") बिल्कुल यही करता है, text को standard output पर भेजते हुए उसके बाद एक newline।

Note: println text के बाद newline जोड़ता है; print नहीं।

उदाहरण: Printing Hello, World

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Hello, World!" to the console, with a trailing newline
    println("Hello, World!")
}

Understanding the Syntax

Curly braces { } function body की शुरुआत और अंत को चिह्नित करते हैं, और अंदर हर statement आमतौर पर अपनी ही line पर बैठता है। कुछ languages के विपरीत, Kotlin को हर line के आखिर में semicolon की ज़रूरत नहीं।

उदाहरण: Understanding the Syntax

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Curly braces group statements together" to the console, with a trailing newline
    println("Curly braces group statements together")
    // Print "No semicolon needed at the end of a line" to the console, with a trailing newline
    println("No semicolon needed at the end of a line")
}

Compiling and Running

code को Hello.kt जैसी file में save करें, फिर इसे kotlinc Hello.kt -include-runtime -d hello.jar से compile करें और अपने terminal में output print होते देखने के लिए result को java -jar hello.jar से चलाएं।

उदाहरण: Compiling and Running

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Saved as Hello.kt, compiled, then run" to the console, with a trailing newline
    println("Saved as Hello.kt, compiled, then run")
}
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. main से पहले fun keyword भूल जाना, जो Kotlin में किसी भी function को define करने के लिए ज़रूरी है।
  2. println को print_ln या Println गलत spell करना, जिससे compiler error आता है क्योंकि Kotlin case-sensitive है।
  3. main function body के लिए closing } छोड़ देना और एक भ्रामक 'expecting a top level declaration' error पाना।
चैप्टर सारांश
  • हर standalone Kotlin program को अपने entry point के रूप में एक fun main() function चाहिए।
  • println() text के बाद newline के साथ print करता है; text को string के रूप में double quotes में wrap होना चाहिए।
  • Kotlin case-sensitive है, इसलिए Main, main, और MAIN तीन अलग identifiers हैं।
  • Kotlin source files आमतौर पर .kt से खत्म होती हैं और kotlinc से compile होती हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.