| System.out.
println(5 * 2 = 10)
|____________________^--------^
You wanted to print 5 * 2 = 10 on the console. However, we see that we cannot pass 5 * 2 = 10 as an
argument to System.out.println() .
5 * 2 = 10 is not a single value. It is a piece of text with numeric characters, * and = .
In Java, we represent text using String . A String literal is a sequence of characters, enclosed within double
quotes: " and " .
jshell> System.out.println("5 * 2 = 10")
| 5 * 2 = 10
Congratulations! You have now figured out how to display not just numbers on the console, but text as well!
Summary
In this step, we:
Were introduced to the System.out.println() method for console output
Used this utility to print a single PMT-Challenge table entry
Step 08: Programming Exercise PE-02
Try and solve the following exercises:
. Print Hello World onto the console.
. Print 5 * 3 , as is.
. Print the calculated value of 5 * 3 .
. Print the number of seconds in a day, using the System.out.println method.
. Do a syntax revision for the code that you write for each of the above exercises. In your code, identify the
following elements:
Numeric and string literals
Expressions
Operators
Operands
Method calls
Step 09: Solutions to PE-02
Solution 1
jshell> System.out.println("Hello World")
Hello World
Solution 2
jshell> System.out.println("5 * 3")
5 * 3
jshell>
Solution 3
jshell> System.out.println(5 * 3)
15
Solution 4
jshell> System.out.println(60 * 60 * 24)
86400
Step 10: Whitespace, Case sensitiveness and Escape Characters
The term whitespace refers to any sequence of continuous space, tab or newline characters.
Whitespace
Let's see a few examples of whitespace in action.
Whitespace affects the output when used in-and-around string literals.
jshell> System.out.println("Hello World")
Hello World
jshell> System.out.println("Hello World")
Hello World
jshell> System.out.println("HelloWorld")
HelloWorld
Whitespace is ignored by the compiler when used around numeric expressions.
jshell> System.out.println(24 * 60 * 60)
86400
jshell> System.out.println(24 * 60 * 60)
86400
jshell>
Case Sensitive
Java is case sensitive.
System.out.println() involve pre-defined Java elements : the System class name, the out variable name,and
the println method name. All are case-sensitive. If any character in these names is used with a different case,
you get an error.
jshell> system.out.println("Hello World")
| Error:
| package system does not exist
| system.out.println("Hello World")
| ^-------^
jshell> System.Out.println("Hello World")
| Error:
| cannot find symbol
| symbol: variable Out
| System.Out.println("Hello World")
| ^------------^
jshell> System.out.Println("Hello World")
| Error:
| cannot find symbol
| symbol: method Println(java.lang.string)
| System.out.Println("Hello World")
| ^---------------------^
jshell>
Inside a string literal, the case of characters do not cause errors. The literal will be taken in and printed, as-is.
jshell> System.out.println("hello world")
hello world
jshell> System.out.println("HELLO WORLD")
HELLO WORLD
Escape Characters
An escape character is a special symbol, used with another regular symbol to form an escape sequence. In Java,
the ' \ ' (back-slash) character plays the role of an escape character. This escape sequence changes the original
usage of the symbols.
If you want to print the string delimiter, the " character, you need to escape it with a \ . Without it, a " character
within a string literal causes an error!
jshell> System.out.println("Hello "World")
| Error:
| ')' expected
| System.out.println("Hello "World")
| ^
jshell> System.out.println("Hello \"World")
Hello "World
jshell>
The escape sequence \n inserts a newline.
jshell> System. out.println("Hello \n World")
Hello
World
jshell> System.out.println("Hello n World")
Hello n World
jshell> System.out.println("Hello\nWorld")
Hello
World
jshell>
The escape sequence \t inserts a tab.
jshell> System.out.println("Hello \t World")
Hello World
jshell> System.out.println("Hello t World")
Hello t World
jshell> System.out.println("Hello\tWorld")
Hello World
jshell>
How do you print a \ ?
jshell> System.out.println("Hello \ World")
| Error:
| illegal escape character
| System.out.println("Hello \ World")
You would need to escape it with another \ . Printing \\ outputs the symbol \ to the console.
jshell> System.out.println("Hello \\ World")
Hello \ World
jshell> System.out.println("Hello \\\\ World")
Hello \\ World
Summary
In this step, we:
Were introduced to method call syntax, with System.out.println()
Discovered the uses of whitespace characters
Learned about Java escape sequences
Step 11: More On Method Calls
Let's look at method calls with a few more examples.
You know how to invoke a method with a single argument, courtesy System.out.println(3*4) . Other scenarios do
exist, such as
Calling a method without any arguments
Calling a method with several arguments
Let's check them out, using the built-in methods in Java Math class.
Method without parameters
In method calls, parentheses are a necessary part of the syntax. Don't leave them out!
Math.random() prints a random real number in the range [0 .. 1] , a different one on each call
jshell> Math.random
| Error:
| cannot find symbol
| symbol: Math.random
| Math.random
| ^------------- ^
jshell> Math.random()
$1 ==> 0.0424279106074651_
jshell> Math.random()
$2 ==> 0.8696879746593543
jshell> Math.random()
$3 ==> 0.8913591586787125
Method with multiple parameters
How do we call Math.min with two parameters 23 and 45 ?
jshell> Math.min 23 45
| Error
| cannot find symbol
| symbol: variable min
| Math.min 23 45
| ^---------^
jshell> Math.min(23 45)
| Error
| ')' expected
| Math.min 23 45
| ---------------^
While making method calls, the programmer must
Enclose all the parameters within parentheses
Separate individual parameters within the list, using the comma symbol ' , '.
jshell> Math.min(23, 45)
$4 ==> 23
jshell> Math.min(23, 2)
$5 ==> 2
jshell> Math.max(23, 45)
$6 ==> 45
jshell> Math.max(23, 2)
$7 ==> 2
jshell>
Math.min() returns the minimum of two given numbers. Math.max() returns the maximum of two given numbers.
Summary
In this step, we:
Understood how zero, or multiple parameters are passed during a method call
Step 12: More Formatted Output
System.out.println() can accept one value as an argument at a maximum.
To display the multiplication table for 5 with a calculated value, we need a way to print both numbers and strings.
For this we would need to use another built-in method System.out.printf() .
When System.out.printf() is called with a single string argument, it prints some illegible information. For now, it
suffices to know, that this information is about the built-in type java.io.PrintStream .
jshell> System.out.printf("5 * 2 = 10")
5 * 2 = 10$1 ==> java.io.PrintStream@4e1d422d
jshell>
The good news is, that if we call the println() method on this, the illegible stuff disappears.
jshell> System.out.printf("5 * 2 = 10").println()
5 * 2 = 10
The method System.out.printf() accepts a variable number of arguments:
The first argument specifies the print format. This is a string literal, having zero or more format specifiers. A
format specifier is a predefined literal (such as %d ), that formats data of a particular type ( %d formats data of
type int ).
The trailing arguments are expressions,
Lots of theory? Let's break it down. Let's look at an example.
jshell> System.out.printf("5 * 2 = %d", 5*2).println()
5 * 2 = 10
jshell>
System.out.printf("5 * 2 = %d", 5*2).println() gives an output 5 * 2 = 10 . %d is replaced by the
calculated value of 5*2 .
Let's play a little more with printf :
jshell> System.out.printf("%d %d %d", 5, 7, 5).println()
5 7 5
Let's try to display a calculated value. In the example below 5*7 is calculated as 35 .
jshell> System.out.printf("%d %d %d", 5, 7, 5*7).println()
5 7 35
Let's use this to print the output in the format that we want to use for multiplication table:
jshell> System.out.printf("%d * %d = %d", 5, 7, 5*7).println()
5 * 7 = 35
Congratulations. We are able to calculate 5*7 and print 5 * 7 = 35 successfully.
Exercise
. Print the following output on the console: 5 + 6 + 7 = 18 . Use three literals 5 , 6 , 7 . Calculate 18 as 5 + 6
+ 7.
Solution
jshell> System.out.printf("%d + %d + %d = %d", 5, 6, 7, 5 + 6 + 7).println()
5 + 6 + 7 = 18
jshell>
Playing with System.out.printf()
In the example below, there are four format specifiers ( %d ) and only three value arguments 5, 6, 7 .
jshell> System.out.printf("%d + %d + %d = %d", 5, 6, 7).println()
5 + 6 + 7 = | java.util.MissingFormatArgumentException thrown: Format specifier '%d'
| at Formatter.format (Formatter.java:2580)
| at PrintStream.format (PrintStream.java:974)
| at PrintStream.printf (PrintStream.java:870)
| at (#52:1)
In a call to System.out.printf() , if the number of format specifiers exceeds the number of trailing arguments, the
Java run-time throws an exception.
If the number of format specifiers is less than the number of trailing arguments, the compiler simply ignores the
excess ones.
jshell> System.out.printf("%d + %d + %d", 5, 6, 7, 8).println()
5 + 6 + 7
jshell>
More Format Specifiers
String values can be printed in System.out.printf() , using the format specifier %s .
jshell> System.out.printf("Print %s", "Testing").println()
Print Testing
Earlier, we used %d to print an int value. You cannot use %d to display floating point values.
jshell> System.out.printf("%d + %d + %d", 5.5, 6.5, 7.5).println()
| java.util.IllegalFormatConversionException thrown: d != java.lang.Double
| at Formatter$FormatSpecifier.failedConversion(Formatter.java:4331)
| at Formatter$FormatSpecifier.printInteger(Formatter.java:2846)
| at Formatter$FormatSpecifier.print(Formatter.java:2800)
| at Formatter.format(Formatter.java:2581)
| at PrintStream.format(PrintStream.java:974)
| at PrintStream.print(PrintStream.java:870)
| at #(57:1)
Floating point literals (or expressions) can be formatted using %f .
jshell> System.out.printf("%f + %f + %f", 5.5, 6.5, 7.5).println()
5.500000 + 6.500000 + 7.500000
jshell>
Summary
In this step, we:
Discovered how to do formatted output, using System.out.printf()
Stressed on the number and sequence of arguments for formatting
Explored the built-in format specifiers for primitive types
Step 13: Introducing Variables
In the previous steps, we worked out how to print a calculated value as part of our multiplication table.
jshell> System.out.printf("%d * %d = %d", 5, 1, 5 * 1).println()
5 * 1 = 5
How do we print the entire multiplication table?
We can do something like this.
jshell> System.out.printf("%d * %d = %d", 5, 1, 5 * 1).println()
5 * 1 = 5
jshell> System.out.printf("%d * %d = %d", 5, 2, 5 * 2).println()
5 * 2 = 10
jshell> System.out.printf("%d * %d = %d", 5, 3, 5 * 3).println()
5 * 3 = 15
jshell> System.out.printf("%d * %d = %d", 5, 4, 5 * 4).println()
5 * 4 = 20
jshell>
Too much work. Isn't it?
If you carefully observe the code, these statements are very similar.
What's changing? The number in the third and fourth parameter slots changes from 1 to 4.
Wouldn't it be great to have something to represent the changing value?
Welcome variables.
jshell> int number = 10
number ==> 10
jshell>
Terminology and Background
In the statement int number = 10 ,
number is a variable.
The literal number is the variable name.
The Java keyword int specifies the type of number .
The literal 10 provided number with an initial value.
This entire statement is a variable definition.
The effects of this variable definition are:
A specific location in the computer's memory is reserved for number .
This location can now hold data of type int .
The value 10 is stored here.
You can change the value of number variable:
jshell> number = 11
number ==> 11
Above statement is called variable assignment.
An assignment causes the value stored in the memory location to change. In this case, 10 is replaced with the
value 11 .
You can look up the value of number variable.
jshell> number
number ==> 11
You can change the value of a variable multiple times.
jshell> number = 12
number ==> 12
jshell> number
number ==> 12
Let's create another variable:
jshell> int number2 = 100
number2 ==> 100
jshell> number2 = 101
number2 ==> 101
jshell> number2
number2 ==> 101
jshell>
The statement int number2 = 100 defines a distinct variable number2 .
How do we use variables to simplify and improve the solution to PMT-Challenge?
Let's take a look.
jshell> System.out.printf("%d * %d = %d", 5, 4, 5*4).println()
5 * 4 = 20
Let's define a variable i , and initialize it to 1 .
jshell>int i = 1
i ==> 1
jshell> i
i ==> 1
jshell> 5*i
$1 ==> 5
Let's update the multiplication table printf to use the variable i.
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 1 = 5
How do we print 5 * 2 = 10 ?
We update i to 2 and execute the same code as before.
jshell> i = 2
i ==> 2
jshell> 5*i
$2 ==> 10
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 2 = 10
jshell>
You can update the value of i to any number.
The previous statement would print the corresponding multiple with 5.
jshell> i = 3
i ==> 3
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 3 = 15
jshell> i = 10
i ==> 10_
jshell> System.out.printf("%d * %d = %d", 5, i, 5*i).println()
5 * 10 = 50
jshell>
By varying the value of i , we are able to print different multiples of 5 with the same statement.
Congratulations! You made a major discovery. Variables.
Summary
In this step, we:
Understood the need for variables
Observed what different parts of a variable definition mean
Seen what goes on behind-the-scenes when a variable is defined
Step 14: Programming Exercise PE-03 (With solution)
. Create three integer variables a , b and c .
Write a statement to print the sum of these three variables.
Change the value of a , and then print this sum.
Then again, change the value of b and print this sum.
Solution to PE-03
jshell>int a = 5
a ==> 5
jshell>int b = 7
b ==> 7
jshell>int c = 11
c ==> 11
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 23
jshell>a = 2
a ==> 2
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 20
jshell>b = 9
b ==> 9
jshell>System.out.printf("a + b + c = %d", a+b+c).println()
a + b + c = 22
jshell>
Step 15: Using Variables
Variables should be declared before use.
jshell>newVariable
| Error:
| cannot find symbol
| symbol: newVariable