0% found this document useful (0 votes)
3 views14 pages

STEM Club Introduction to Java _ PrintOut_Output

The document provides an overview of printing in Java, detailing the use of the println() and print() methods for outputting text and numbers. It emphasizes the importance of using double quotes for text and demonstrates how to include comments in code for better readability. Additionally, it covers the ability to perform mathematical calculations within the println() method and explains single-line and multi-line comments.

Uploaded by

mandla.sithole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

STEM Club Introduction to Java _ PrintOut_Output

The document provides an overview of printing in Java, detailing the use of the println() and print() methods for outputting text and numbers. It emphasizes the importance of using double quotes for text and demonstrates how to include comments in code for better readability. Additionally, it covers the ability to perform mathematical calculations within the println() method and explains single-line and multi-line comments.

Uploaded by

mandla.sithole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Printing

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/

With double quotes – will work


System.out.print("Hello World!");

Without double quotes – will not work


STEM System.out.print(Hello World!);
Printing
Java Output/Print?
Text
There is also a print() method,
which is similar to println().
The print() Method
The only difference is that it
does not insert a new line at Example
the end of the output:
System.out.print("Hello World! ");
System.out.print("I will print on the same line.");

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

www.online-java.com This example uses a single-line comment at the


end of a line of code:

System.out.println("Hello World"); // This is a comment

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

www.online-java.com This example uses a single-line comment at the


end of a line of code:

System.out.println("Hello World"); // This is a comment

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

/* The code below will print the words Hello World


Any text between /* and */ to the screen, and it is amazing */
will be ignored by Java. System.out.println("Hello World");

This example uses a multi-line comment (a


comment block) to explain the code:
STEM

You might also like