आपका पहला Kotlin Program
In this page:
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
// 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()")
}
Login to try C/C++/Java/PHP code in the editor
Printing Hello, World
किसी भी language में classic पहला program screen पर एक greeting print करता है। Kotlin में, println("Hello, World!") बिल्कुल यही करता है, text को standard output पर भेजते हुए उसके बाद एक newline।
println text के बाद newline जोड़ता है; print नहीं।उदाहरण: Printing Hello, World
// Entry point: execution of the program starts here
fun main() {
// Print "Hello, World!" to the console, with a trailing newline
println("Hello, World!")
}
Login to try C/C++/Java/PHP code in the editor
Understanding the Syntax
Curly braces { } function body की शुरुआत और अंत को चिह्नित करते हैं, और अंदर हर statement आमतौर पर अपनी ही line पर बैठता है। कुछ languages के विपरीत, Kotlin को हर line के आखिर में semicolon की ज़रूरत नहीं।
उदाहरण: Understanding the Syntax
// 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")
}
Login to try C/C++/Java/PHP code in the editor
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
mainसे पहलेfunkeyword भूल जाना, जो Kotlin में किसी भी function को define करने के लिए ज़रूरी है।printlnकोprint_lnयाPrintlnगलत spell करना, जिससे compiler error आता है क्योंकि Kotlin case-sensitive है।mainfunction 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: