Lesson 2
Print Statement and Math Operations
Author: Brandon Fajardo (8/29/17). Updated (6/18/20)
Introduction
Review: In the first lesson, we first learned how to set up a basic class. Then, we learned many things about storing data with variables and their data types.
Lesson topic: In this lesson, we will learn how to print code. Then, we will learn how to use basic mathematical operators in code.
Topic 1 - Print Statement
When we code, sometimes we wish to output certain pieces of information. In Java, we are able to “print” information out using print statements.
Print statement: a statement which outputs data into the console when we run our code.
Notation:
This will output what is written within the parenthesis () into the console.
Console: the window which appears when code is being run to display any outputs of the code.
The console will also display errors that have occurred in the processing of running the code.
Subtopic 1 - Printing Values Directly
The values written within the set of parentheses of a print statement will be output.
EX:
Activity: Print out the famous programming phrase: Hello, World!
If multiple print statements are used, the outputs will combine on the same line.
Subtopic 2 - Printing Variables
You can also output the value associated with a given variable using a print statement. This is done by referencing the variable’s name. This is similar to what we would expect in using variables in mathematics.
EX:
EX:
Things to note: Many char images do not accurately display in the console. Many show images of little boxes as a default image instead. To see all possible chars, search the internet for the Unicode characters.
Subtopic 3 - Multiple Line Printing
Currently, if we print multiple things at once, the outputs jumble up together into one line. We can fix this by forcing our outputs to print on separate lines in the console.
The following notation is used:
“ln” is added to the end of the word. This stands for, new line.
Things to note: The shortcut for this multiple line print statement is sout (written) + tab/enter.
Subtopic 4 - Concatenation
Concatenation, combining multiple values to make a larger String. We can combine values together using addition with the + symbol. This can be done within a print statement as well.
EX:
Numbers can also be operated within print statements, and the solution will output.
EX:
Topic 2 - Mathematical Operations
In Java, we also utilize mathematical operations. For the most part, they do what you would normally expect them to do.
Here is a basic list of what we use:
Subtopic 1 - Modulus
Modulus: returns the whole number remainder after dividing.
EX: 7%4 = 3
Subtopic 2 - Quirk of Division
Java is different from traditional math and some other programming languages in that it treats integers and decimal values as distinct data types. As a result, certain quirks in your mathematical operations may appear if they are not well checked.
For example, when you divide two variables of integer data types, the result will always be an integer. This is despite if, in reality, the result should be some decimal value.
EX:
To be able to get back decimal values from division, at least one number must be of a decimal type.
EX:
Subtopic 3 - Quirks of Multiplication (0 decimals)
In multiplication, even if the result is a whole number, if there is an operand which can hold or has a decimal point in the operand, the console will output the number with a decimal of .0 This also has to do with the fact that the result is treated as a decimal rather than an integer data type.
EX:
Conclusion
Recap: In this lesson, we covered how to print values from code and how to use basic mathematical operations.
Next: In the next lesson, we will learn if statements with a focus on their conditions. This uses mathematical operators such as inequalities (>, <=).
Last updated