JavaProgrammingForBeginners-CourseBook-11-20
JavaProgrammingForBeginners-CourseBook-11-20
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
Solution 2
jshell> System.out.println(5 * 3)
15
Solution 4
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>
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!
You would need to escape it with another \ . Printing \\ outputs the symbol \ to the console.
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
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
| ---------------^
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.
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
Solution
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)
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
jshell> number = 11
number ==> 11
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>
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>
jshell>newVariable
| Error:
| cannot find symbol
| symbol: newVariable