Keywords and symbols look similar, but they play very different roles. Understanding the difference is a big “Java → Clojure” reading milestone.
Keywords (:k)
Keywords are immutable identifiers that evaluate to themselves. They’re most commonly used as map keys.
1:status
2:user/id ; namespaced keyword (helps avoid collisions)
Two idioms you’ll see constantly:
1(def m {:status :ok})
2
3(:status m) ; => :ok ; keyword-as-function lookup
4(get m :status)
Symbols (foo)
Symbols are names that usually resolve to vars (functions or values) when evaluated.
1(def status :ok)
2
3status ; => :ok ; resolves to a var’s value
4'status ; => status ; quoted symbol (data, not lookup)
Symbols show up everywhere because code uses symbols:
1(+ 1 2) ; here `+` is a symbol that resolves to a function
Why Namespaced Keywords Matter
In Java, “package + class name” avoids collisions. In Clojure, namespaced keywords like :user/id or :order/id help avoid accidental key collisions when you merge maps from different parts of a system.
Knowledge Check: Keywords vs Symbols
### Which statement is true about keywords?
- [x] A keyword like `:k` evaluates to itself and is commonly used as a map key.
- [ ] A keyword like `:k` must be defined with `def` before it can be used.
- [ ] Keywords resolve to vars in a namespace.
- [ ] Keywords are mutable identifiers like Java variables.
> **Explanation:** Keywords are self-evaluating values and are widely used for map keys and “keyword-as-function” lookup.
### What does quoting a symbol do? (Example: `'status`)
- [x] It returns the symbol as data instead of resolving it to a value.
- [ ] It turns the symbol into a keyword.
- [ ] It evaluates the symbol in a different namespace.
- [ ] It forces compilation of the symbol.
> **Explanation:** Quote prevents evaluation. You’ll see quoted symbols and lists constantly in macros and code tools.
### Why might you prefer `:user/id` over `:id` as a map key in a larger system?
- [x] It reduces key collisions when maps from different domains get merged or passed around.
- [ ] It makes map lookups faster on the JVM.
- [ ] It makes keywords mutable.
- [ ] It avoids the need for `get`.
> **Explanation:** Namespacing communicates intent and reduces accidental collisions across modules and teams.