Print Methods
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-1-1.png)
The print()
method prints the data inside the parenthesis out to the screen. Data can be a text enclosed in quotes or a number.
System.out.print("Area");
System.out.print(51);
Area51
The println()
method prints data just as the print()
method. One difference is that the println()
moves the cursor to a new line after printing.
System.out.println("Area");
System.out.println(51);
Area
51
|
The other difference is that the println()
allows no argument. No argument is denoted by empty parenthesis. This outputs an empty line.
System.out.println("Area");
System.out.println(); // empty line
System.out.println(51);
Area
51
|
On the other hand, the print()
method used without an argument result in a syntax error. It must have an argument.
System.out.print(); // error
Code Example
In this section, you’ll play with a sample code using the online development tool Repl.it
(or any development tool of your choice).
- Change the text from “Hello world!” to “Hello Java”.
- Add a comment.
- Run the program.
Step 1: Launch Repl.it
. The following sample code is provided for you. If it’s not, you can copy and paste this code to a new file and save it as “Main.java
”.
class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Change the text inside the parenthesis from “Hello world!
” to “Hello Java
”. Be sure the text is enclosed in double quotes.
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Step 2: Insert a new line by placing the cursor after the semicolon (;) and pressing the enter key.
In the new line, enter // (double slashes). Notice how Repl.it
tool immediately recognizes comment by changing the text color. Enter the comment text.
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
// Comment: I changed the program.
}
}
Step 3: Now run the program by clicking the run button. This is what you see in the output panel.
Hello Java
In-Depth Knowledge
System.out.print
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-6.png?w=500)
System Class
Java provides prewritten classes which contain codes for common operations in general programming. The System class is one of the prewritten classes.
Out Object
The System class specifies out
object which represents an “output stream”. The output stream sends data to the output destination. In this case, the output destination is the screen.
Print Method
The out object has associated methods: print()
and println()
. Data is the argument passed into the method, and that data is exactly what is output to screen.
There are several versions of print method. Each version allows different kind of data as argument.
System.out.print(true); // prints true
System.out.print('A'); // prints letter A
System.out.print(3.14159); // prints 3.14159
System.out.print("Hello Java"); // prints Hello java
In addition, println()
method allows no argument.
System.out.println(); // prints empty line
String Literal
A character is any one letter, digit, or symbol enclosed in single quotes: 'A', '1', '@'
.
String is a sequence of characters. Think of String as characters on a string.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-7.png?w=600)
String literal is a string that begins with a double quote and ends with a double quote. Single quote cannot be used. String literal can be zero length, and this is called an “empty string”.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-8.png?w=600)
The line terminator cannot appear between the opening and closing quotes. In other words, String literal must be in one line. Otherwise, a compile-time error occurs.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-9.png?w=600)
String literal is the literal representation of a String. The computer represents it as-is. In other words, the value you input is what you get in the output. The value doesn’t change, so String literal is a constant value.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-10.png?w=600)
Two String literals can be appended together to form a new String literal. This is called string concatenation. String literal can be appended with a number as well.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-11.png?w=600)
System.out.println("Hi" + " there!"); // prints Hi there!
System.out.println("12" + 3); // prints 123
Other Literals
There are other literals besides the String literal.
![](https://precollegejava.wordpress.com/wp-content/uploads/2022/07/cs4-12.png?w=600)
Consider these two statements. They output the same value.
System.out.println("A"); // prints A
System.out.println('A'); // prints A
Now consider these two statements. They output the same value.
System.out.println("365"); // prints 365
System.out.println(365); // prints 365
The output may look the same to human eyes, but the computer treats them differently. This is because each literal is classified differently and is represented differently in the computer system.
In the upcoming chapters, we’ll cover how Java classifies different kinds of data and how they are represented in the computer memory. We’ll also cover objects and introduce String class which represents the String literal.
2022 Copyright