Your first useful cell
1.1.1 — Your first useful cell
Learn how to run Java code, print text, and calculate numbers. Each example includes the code you need.
A code cell is an editable box of Java code. Click it and choose Run cell. The result appears underneath.
Using this notebook
Run the examples from top to bottom. After changing code, run the cell again to see the new result.
The notebook can run a single expression such as 6 * 7, or statements such as System.out.println("Hello");. A complete Java application also needs surrounding structure; this notebook supplies it for you.
Your edits and quiz answers last only while this lesson is open. Leaving or reloading starts a fresh session.
Your first calculation
Run the code cell below using Run cell. Look at the output that appears underneath it.
The * symbol means multiplication in Java. An expression is a piece of code that produces a value. Here, 6 * 7 produces a number, and the notebook displays that number.
Read the result
The output is 42: Java multiplied six by seven. The output is the result; 6 * 7 is the expression you asked Java to evaluate.
Change one thing
Go back to the 6 * 7 code cell and change the 6 to 9. Predict the result, then run the cell again to check it.
Changing the expression changes the result. Java calculates using the numbers currently in the cell.
Print text
A String is a text value written inside double quotes, such as "Hello". The quotes belong to the code; they are not printed.
System.out.println(...) prints the value inside the parentheses and moves to a new output line. Spell System with an uppercase S and println in lowercase.
This call is a statement: an instruction for Java to perform. The semicolon ; ends it.
Replace Hello, Java! with Hello, world! and run the cell again. Keep the quotes, parentheses, and semicolon. Only the printed words should change.
Two instructions, two lines
A code cell can contain several statements. Java performs these simple statements from top to bottom.
Predict the order of the two output lines before running this example.
Reverse the two statements and run the cell. Second line should now appear first.
The source is the code you edit. The output is what the program prints when you run it.
Numbers and text behave differently
+ adds numbers. Inside quotes, the same characters are text.
Run both cells below. The first calculates 18 + 12; the second prints the characters 18 + 12.
Join text and a number
When a value on either side of + is a String, Java joins text instead of adding numbers. This is called concatenation.
In "Result: " + (18 + 12), the parentheses group the addition. Java calculates 30, then joins it to the label. The space after the colon separates the label from the number.
Comments
// starts a comment that continues to the end of the source line. Java ignores it when running the code.
Use comments to explain something useful to the reader. A comment does not print anything.
Practice: print three lines
Write code in the next cell to produce:
Hello, Java!
Sum: 40
Product: 48Use 24 + 16 for the sum and 6 * 8 for the product. Join each calculated number to its label. Use three println statements.
Hints
- Put printed text inside double quotes.
- Use
+to join a label and a calculated number. - Group each calculation in parentheses:
"Sum: " + (24 + 16).
Check your output against the three required lines.
Worked solution
System.out.println("Hello, Java!");
System.out.println("Sum: " + (24 + 16));
System.out.println("Product: " + (6 * 8));Each statement prints one line. The parentheses keep the arithmetic together before its result is joined to text.
Try changing 24 to 27. Run the cell again and check which output line changes.
Check your code
Check the text, line order, and calculated results. Change one number and run the code again.
You should be able to explain what quotes, println, +, *, and // do in these examples.
Enable JavaScript to run the Java notebook.