Hello World¶
Let's write and run your first Onion program!
The Classic Hello World¶
Create a file named Hello.on:
That's it! This single line is a complete Onion program.
Running Your Program¶
There are two ways to run Onion programs:
Method 1: Direct Execution with onion¶
The onion command compiles and runs your program in memory:
Output:
Method 2: Compilation with onionc¶
Compile to .class files and run with Java:
Understanding the Code¶
IO- The built-in I/O module::- Static method access operator (similar to Java's.)println()- Print with newline function"Hello, World!"- String literal
More Examples¶
Using Variables¶
Getting User Input¶
Run it:
Output:
With a Main Class¶
You can also write programs with explicit class definitions:
This follows the traditional Java main method pattern.
Command-Line Arguments¶
Access command-line arguments through the main method:
class ArgsDemo {
public:
static def main(args: String[]): void {
println("Number of arguments: " + args.length)
for var i: Int = 0; i < args.length; i = i + 1 {
println("Arg " + i + ": " + args[i])
}
}
}
Run with arguments:
Output:
Next Steps¶
Now that you've written your first Onion program, continue with:
- Quick Start Guide - Learn essential language features
- Basic Syntax - Variables, types, and operators