STEM Club Introduction to Java _ PrintOut_Output
STEM Club Introduction to Java _ PrintOut_Output
Java Output/Print?
Text
You learned from the previous
chapter that you can use the
The println() Method
println() method to output
values or print text in Java:
Example
System.out.print("Hello World!");
STEM
Printing
Java Output/Print?
Text
You can add as many println() The println() Method
methods as you want.
Print or display several line
Note that it will add a new line
Try it out!
for each method:
https://www.online-java.com/
System.out.println("Hello World!");
System.out.println("I am learning Java.");
System.out.println("It is awesome!");
STEM
Printing
Java Output/Print?
Text
Text must be wrapped inside
double quotations marks "".
Double Quotes (“”)
If you forget the double Try it out these examples!
quotes, an error occurs: https://www.online-java.com/
STEM
Printing
Java Output/Print?
Text
Note that we add an extra
space (after "Hello World!" in
The print() Method
the example) for better
readability.
Example – Try example without the space.
Remove space and try the
code again. System.out.print("Hello World!");
System.out.print("I will print on the same line.");
STEM
Printing
Java Output/Print?
Numbers
You can also use the println()
method to print numbers.
Use println() to print
However, unlike text, we don't
numbers
put numbers inside double
quotes: Example – Try it Out!
www.online-java.com
System.out.println(300);
System.out.println(1050);
STEM
Printing
Java Output/Print?
Numbers
You can also perform
mathematical calculations
Math Calculations
inside the println() method:
System.out.println(3+3);
Examples System.out.println(3-3);
– Try them Out at
System.out.println(3*3);
www.online-java.com!
System.out.println(3/3);
STEM
Java
Java Comments
Comments
Comments can be used to
explain Java code, and to
Single-line comments
make it more readable.
… start with two forward slashes (//).
It can also be used to prevent
execution when testing Any text between // and the end of the line is
alternative code. ignored by Java (will not be executed).
Example:
// This is a comment
System.out.println("Hello World");
STEM
Java
Java Comments
Comments
Single-line comments
Single-line comments
Try the examples
Example 1: Write and run the code
www.online-java.com
This example uses a single-line
comment before a line of code:
// This is a comment
System.out.println("Hello World");
STEM
Java
Java Comments
Comments
Single-line comments
Single-line comments
Try the examples
Open online java IDE … Example 2: Write and run the code
STEM
Java
Java Comments
Comments
Single-line comments
Single-line comments
Try the examples
Open online java IDE … Example 2: Write and run the code
STEM
Java
Java Comments
Comments
Multi-line Comments
Multi-line Comments
Multi-line comments start with
/* and ends with */. Example: Write and run the code