Hello World — Deep Dive
Hello World — Deep Dive
Every programmer writes "Hello, World!" as their first program. Most tutorials show you what to type, but rarely explain why each piece is written the way it is. In this lesson, we go deep — dissecting every character of the simplest Dart program so you truly understand what's happening.
The Complete Hello World Program
dartvoid main() { print('Hello, World!'); }
That's just 3 lines. But there's a lot to unpack. Let's go piece by piece.
Part 1: void — The Return Type
dartvoid main() { ... } ^^
void is a type keyword in Dart. It means: "this function does not return any value."
In Dart (as in many languages), every function must declare what kind of value it returns. When a function doesn't return anything meaningful, you use void to make that explicit.
Contrast with functions that return values:
dart// Returns an integer int add(int a, int b) { return a + b; } // Returns a String String greet(String name) { return 'Hello, $name!'; } // Returns nothing (void) void sayHello() { print('Hello!'); // No return statement needed }
Why not just leave out the type? Dart is a statically typed language, which means types matter. Declaring void is a contract: callers know this function won't give them a value back. It also helps the Dart analyzer catch bugs early.
Part 2: main — The Entry Point
dartvoid main() { ... } ^^^^
main is not just any function name — it is the special entry point of every Dart program.
When you run dart run myfile.dart, the Dart runtime looks for a function named exactly main and begins execution there. It's the designated "start here" marker.
The rules for main:
- Must be named exactly
main(lowercase) - Must be a top-level function (not inside a class)
- Must have either no parameters or exactly one parameter:
List<String> args
dart// Simplest form — no arguments void main() { print('Simple main'); } // With command-line arguments void main(List<String> args) { print('Arguments passed: $args'); }
Passing arguments from the terminal:
bashdart run main.dart hello world 123
dartvoid main(List<String> args) { print(args); // [hello, world, 123] }
What happens without main? You get an error:
Error: No 'main' method found.
Dart simply doesn't know where to start.
Part 3: () — The Parameter List
dartvoid main() { ... } ^^
The parentheses () define the parameter list — the inputs the function accepts. For main() with no arguments, it's empty.
This is a consistent Dart pattern:
dartfunctionName(parameter1, parameter2) { ... }
Even when there are no parameters, the parentheses are required — they signal to Dart (and to the reader) that main is a function, not a variable.
Part 4: { } — The Function Body
dartvoid main() { print('Hello, World!'); } ^ ^
The curly braces define the function body — the block of code that runs when the function is called. Everything between { and } belongs to main.
Dart uses curly-brace blocks everywhere: functions, if statements, loops, classes — they all use {} to group code.
Part 5: print() — Output to Console
dartprint('Hello, World!'); ^^^^^
print is a built-in top-level function from Dart's dart:core library. It writes its argument to the standard output (your terminal), followed by a newline character.
You don't import it. You don't configure it. It's always available.
How print() works internally:
- Takes any
Objectas its argument - Calls
.toString()on it (converts it to a String) - Writes the result to
stdout(standard output stream) - Appends a newline
\ncharacter
print() with different types:
dartvoid main() { print('A string'); // A string print(42); // 42 print(3.14); // 3.14 print(true); // true print([1, 2, 3]); // [1, 2, 3] print({'key': 'value'}); // {key: value} print(null); // null }
Difference between print() and stdout.write():
dartimport 'dart:io'; void main() { print('With newline'); // adds \n automatically stdout.write('Without newline'); // no automatic newline stdout.write(' - same line\n'); // manual newline }
Output:
With newline
Without newline - same line
Part 6: Strings in Dart
dartprint('Hello, World!'); ^^^^^^^^^^^^^^^
'Hello, World!' is a String literal — a sequence of characters enclosed in quotes.
Single Quotes vs Double Quotes
Dart accepts both — they are completely equivalent:
dartvoid main() { print('Hello with single quotes'); // ✅ Valid print("Hello with double quotes"); // ✅ Valid }
When to prefer one over the other:
Use single quotes by default (Dart community convention).
Use double quotes when your string contains a single quote:
dartprint("It's a great day!"); // ✅ Avoids escaping print('It\'s a great day!'); // ✅ Also valid, but needs escape
Use single quotes when your string contains a double quote:
dartprint('She said "hello"'); // ✅ Clean print("She said \"hello\""); // ✅ But needs escape
Triple Quotes — Multi-line Strings
For strings that span multiple lines, use triple quotes:
dartvoid main() { String poem = ''' Roses are red, Violets are blue, Dart is awesome, And so are you! '''; print(poem); }
Triple double quotes also work:
dartString html = """ <html> <body>Hello</body> </html> """;
String Interpolation
Embed variables or expressions directly inside a string using $:
dartvoid main() { String name = 'Dart'; int version = 3; print('Hello from $name $version!'); // Hello from Dart 3! print('1 + 1 = ${1 + 1}'); // 1 + 1 = 2 print('Name length: ${name.length}'); // Name length: 4 print('Uppercase: ${name.toUpperCase()}'); // Uppercase: DART }
Rule: Use $variableName for simple variables. Use ${expression} for anything more complex.
Part 7: The Semicolon ;
dartprint('Hello, World!'); ^
The semicolon terminates a statement in Dart. Every statement must end with ;.
What is a statement? A complete instruction — something Dart executes.
dartvoid main() { print('First statement'); // ← statement 1 int x = 10; // ← statement 2 x = x + 5; // ← statement 3 print(x); // ← statement 4 }
Missing semicolon? Dart gives a clear compile-time error:
Error: Expected ';' after this.
Running Hello World
Save the code in a file named main.dart (or any .dart name):
dartvoid main() { print('Hello, World!'); }
Run it from your terminal:
bashdart run main.dart
Output:
Hello, World!
Hello World Variations
Now that you understand the fundamentals, let's explore different ways to write Hello World:
Variation 1 — Variable + Interpolation
dartvoid main() { String message = 'Hello, World!'; print(message); }
Variation 2 — With Computation
dartvoid main() { String language = 'Dart'; int year = 2011; print('$language was created in $year'); // Dart was created in 2011 }
Variation 3 — Multiple Lines
dartvoid main() { print('Line 1: Hello!'); print('Line 2: Welcome to Dart.'); print('Line 3: Let\'s build something great!'); }
Variation 4 — Using stdout
dartimport 'dart:io'; void main() { stdout.writeln('Hello, World!'); // Same as print stdout.write('No newline here'); stdout.write(' — still same line\n'); }
Variation 5 — Multiline String
dartvoid main() { print(''' ┌─────────────────────┐ │ Hello, World! │ │ Welcome to Dart! │ └─────────────────────┘ '''); }
Common Errors in Hello World
❌ Missing Semicolon
dartvoid main() { print('Hello, World!') // ← Missing ; }
Error: Expected ';' after this.
Fix: Add ; after every statement.
❌ Wrong Function Name
dartvoid Main() { // ← Capital M — wrong! print('Hello'); }
Error: No 'main' method found.
Fix: Use lowercase main.
❌ Missing Parentheses on print
dartvoid main() { print 'Hello'; // ← Missing () }
Error: Expected ';' after this.
Fix: print('Hello'); — always include ().
❌ Mismatched Quotes
dartvoid main() { print('Hello"); // ← Started with ' ended with " }
Error: String starting with quote 'Hello" contains invalid character.
Fix: Match opening and closing quote style.
❌ Missing void or Return Type
dartmain() { // ← Missing return type print('Hello'); }
This actually works in Dart (it infers dynamic), but it's bad practice. Always declare void main() explicitly.
Summary
In this deep dive, you learned:
| Part | Meaning |
|---|---|
void | The function returns no value |
main | The entry point — where execution begins |
() | Empty parameter list |
{ } | The function body block |
print() | Built-in function to output to the console |
'...' / "..." | String literals (single or double quotes) |
; | Statement terminator — required after every statement |
Next Up: Now that you understand every piece of Hello World, let's zoom out and look at the full structure of a Dart program — imports, classes, top-level functions, and more.