Hello World in J
Your first J program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. In J, this is remarkably concise—just a string expression that outputs to the screen.
The Code
Create a file named hello.ijs:
| |
That’s it—one line that writes our greeting to standard output.
Understanding the Code
Let’s break down this concise expression:
'Hello, World!'- A character string (called a “literal” in J)1!:2- A “foreign conjunction” for file output (the1class handles files,:2means write)(2)- J file number 2 represents screen output (note: this is J’s own numbering, not Unix file descriptors)
In J, !: is the foreign conjunction that provides access to system functions. The 1!:2 specifically means “write to file,” and J file number 2 is screen output.
Alternative Approaches
J offers several ways to output text:
| |
The NB. is J’s comment marker (“Nota Bene” - Latin for “note well”).
Running with Docker
The easiest way to run J without installing anything locally:
| |
Command Line Options Explained
-v $(pwd):/app- Mount current directory into the container-w /app- Set working directory inside containerhello.ijs- The script file to execute (passed to the image’s defaultijconsoleentrypoint)
Running Locally
If you have J installed locally:
| |
Installing J
All Platforms:
Download from Jsoftware.com - J is available for Windows, macOS, Linux, and Raspberry Pi.
Linux (from source):
| |
Expected Output
Hello, World!
Interactive J
J is particularly powerful in interactive mode. Start a J session:
| |
Then type expressions and see immediate results:
| |
The three-space indent shows user input in J’s interactive mode.
To exit, type exit '' or press Ctrl+D.
Going Beyond Hello World
J’s power becomes apparent with array operations. Even in a “Hello World” tutorial, we can glimpse this:
| |
The single character |. reverses the entire string—no loops, no indexing.
| |
The # verb counts the elements (characters in a string).
| |
The $ (reshape) verb rearranges our string into a 3×4 matrix.
A Note on J Syntax
J’s ASCII-based syntax uses punctuation creatively:
| Symbol | Meaning | Example |
|---|---|---|
+ | Add | 2 + 3 → 5 |
- | Subtract/Negate | 5 - 3 → 2, - 5 → _5 |
* | Multiply/Sign | 2 * 3 → 6 |
% | Divide | 6 % 2 → 3 |
# | Count/Copy | # 'hi' → 2 |
$ | Shape/Reshape | $ 'hi' → 2 |
i. | Integers | i. 5 → 0 1 2 3 4 |
!: | Foreign (system) | 1!:2 = file write |
Notice that J uses % for division (not /) and _ for negative numbers (not -).
Key Concepts
- ASCII-only - J uses standard keyboard characters, unlike APL
- Foreign conjunctions -
m!:nprovides system-level operations - Right-to-left evaluation - No operator precedence except right-to-left
- Arrays are fundamental - A string is a character array
- Verbs work on arrays - No explicit iteration needed
Historical Note
J was created by Kenneth Iverson and Roger Hui in 1989-1990 as a modernization of APL. The switch from APL’s special characters to ASCII was revolutionary—suddenly APL’s powerful array programming was accessible on any computer with any keyboard.
According to Roger Hui, the name “J” was chosen simply because “it is easy to type” — Hui picked it when saving the first source code file.
Running Today
All examples can be run using Docker:
docker pull nesachirou/jlang:latest
Comments
Loading comments...
Leave a Comment