Core Java · Chapter 1 of 48
Hello, Java
By the end of this chapter you can write and run a Java program that prints lines, and write small methods that return text. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 5 tasks
- Java
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
Tapri has won the contract to run chai counters at railway stations, from Bengaluru (SBC) to New Delhi (NDLS) and Mumbai (CSMT). The railways run on Java, so every counter will run Tapri Counter, a Java program you write one ticket at a time. Meera's first ticket: when a counter opens in the morning, its screen says so and shows the menu, the same at every station.
Today each counter boy chalks the menu on a board, and no two boards agree: one says ₹40, another ₹45, one forgets the bun maska. A program prints the same lines everywhere, but only if you know how a Java program starts, how it prints, and how one piece of code hands text to another.
The lesson
A Java file, line by line
Here is the smallest useful program, for the platform board:
public class Board {
public static void main(String[] args) {
System.out.println("Platform board is live");
}
}
Run it in the terminal, in the folder that holds the file, with java Board.java:
Platform board is live
- Java code lives in a class. A public class goes in a file with the same name, so
Boardlives inBoard.java. mainis where the program starts, and its shape is fixed:public static void main(String[] args).System.out.println(...)prints a line. Every statement ends with;.//starts a comment to the end of the line, and/** ... */above a method describes it.
For one file, java compiles it in memory and runs it; big projects compile first with javac. Either way the JVM runs the bytecode, so the same program runs on Linux, macOS and Windows.
Forget the semicolon and javac stops, pointing at where it expected one:
Board.java:3: error: ';' expected
System.out.println("Platform board is live")
^
1 error
error: compilation failed
Java also cares about capitals. Write system.out.println with a small s and you get error: package system does not exist.
Methods that return a value
Printing is for people. Most code returns a value instead, so other code (and the tests) can use it:
static String farewell(String name) {
return "See you tomorrow, " + name;
}
String is the return type and name is a parameter. + joins strings, and numbers join too:
System.out.println(farewell("Ravi"));
System.out.println("Train " + 12627 + " is on time");
See you tomorrow, Ravi
Train 12627 is on time
Text between quotes is copied exactly, so a missing space shows up as two words run together.
A method that says it returns a String must return one. Print inside it instead of returning, and javac refuses:
Board.java:8: error: missing return statement
}
^
1 error
error: compilation failed
Data can sit in the class too. A List holds items in order, and an array holds numbers; both count from 0 (chapters 7 and 27 explain them):
static final List<String> TRAINS = List.of("Rajdhani", "Shatabdi");
static final int[] PLATFORMS = { 4, 1 };
TRAINS.get(1) + " on platform " + PLATFORMS[1] prints Shatabdi on platform 1.
print, println and printf
println prints its text and then ends the line. print prints its text and the cursor stays where it is, so the next text lands on the same line. println() with nothing inside only ends the line. Press Next to watch the screen fill:
printf fills in blanks: %s takes a string, %d a whole number, and %n ends the line.
System.out.printf("%s leaves at %d am%n", "Shatabdi", 6);
Shatabdi leaves at 6 am
Counter.java has a main, so you can try your code at any time with java Counter.java.
Your turn
Your five tasks are at the top, all in Counter.java. Copy the text formats exactly, spaces and the rupee sign included, and in the menu make sure each item ends its own line. The last task is Kabir's interview question: work out which lines print and println leave on the screen.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Counter.java: print the welcome line
In Counter.java, make main print exactly one line, Tapri Counter is open, and end it with a line break. Print nothing else. The capital letters and spaces must match.
For example, running java Counter.java shows Tapri Counter is open and nothing more.
- Task 2
Counter.java: greet a station by its code
In Counter.java, make greeting(station) return the text Welcome to Tapri, then a comma and a space, then the station code it was given. It returns the text; it does not print it.
For example, greeting("SBC") returns Welcome to Tapri, SBC and greeting("NDLS") returns Welcome to Tapri, NDLS.
- Task 3
Counter.java: build one menu line
In Counter.java, make priceLine(item, price) return one menu line: the item, a colon and a space, the rupee sign ₹, then the price, with no space between the rupee sign and the price. It returns the text; it does not print it.
For example, priceLine("Masala chai", 40) returns Masala chai: ₹40 and priceLine("Parle-G", 10) returns Parle-G: ₹10.
- Task 4
Counter.java: print the whole menu
In Counter.java, make printMenu() print the menu: one line per item, in the order of ITEMS, each line being the priceLine of that item and its price from PRICES, and each line ending with a line break. Print nothing else, no title and no empty line.
For example, it prints Masala chai: ₹40, then Cutting chai: ₹25, then Bun maska: ₹50, each on its own line.
- Task 5 · interview question
Interview: Counter.java, predict what print and println show
In Counter.java, make predictOutput() return the lines that the code in its comment leaves on the screen, one string per line, in order, without the line breaks.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- A class and its main method: where every Java program starts.
- Methods that return a value: code that builds text for other code, joining words and numbers with +.
- print, println and printf: what each one leaves on the screen.
More in First steps
Build "Hello, Java" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.