Java: Displaying Output and String Concatenation

In this lesson, we will go over displaying output and string concatenation in Java.       

Displaying Output

The concept of displaying the outputs to the users was already explored in a few other lessons.   

To reiterate, either the print() method or the println() method can display outputs to the users. Java itself provides these methods, and they can be used at any point by integrating System.out right before the methods. System.out is used to access these two methods, which are present in the PrintStream class. Classes and methods will be elaborately discussed in an upcoming lesson.

Let us understand why there are two methods to display the outputs. The println() method moves the cursor to the next line once a particular message is displayed while print() does not.  

Let us take an example by writing 

System.out.println("Good Evening!");
System.out.println ("I hope you are having a great day!");

Our resultant output will be 

Good Evening!
I hope you are having a great day!

Instead, typing out

System.out.print("Good Evening! ");
System.out.print("I hope you are having a great day!");

will result in the following output

Good Evening! I hope you are having a great day!

This difference is the minor variation between these two methods. Apart from this, both of them have the same functionality. 

The lesson will now delve a little deeper into the use of the println() method to display messages to users. Note that the print()method functions in a very similar way, barring the stated minor difference.  

Displaying a simple text message

To display a simple message, just follow this format:

System.out.println("Hello, I am Sandra.");

Consequently, the programmer’s output will be:

Hello, I am Sandra.

Displaying the value of a variable

In case programmers have a variable that they wish to display to their users, the variable name can be used as an argument.

For example:

int example = 77;

now, the value of example can be displayed by typing this:

System.out.println(example);

The output will be:

77

Please observe that the variable name (example) has not been enclosed in double-quotes. In case double quotes are used in this manner:

System.out.println("example");

The result will be

example

Displaying results by skipping the step of assigning data to variables

If programmers want to find the result of a direct mathematical expression, they need not use a variable in the println() method. It can be done by typing this out as an argument to the method.  

For example, this result can be achieved by writing

System.out.println(80 + 6);

resulting in the output:

86

To display the result of a method, this is the process

System.out.println("Players".substring(2, 4));

The substring of the word “Players” is taken in the code and displayed as a result. The result would be: 

ay

Making the most of the concatenation sign

Now, the lesson will discuss the process of displaying strings by merging several strings. For that, the concatenation sign plays a very vital role.  

For example, this can be written: 

System.out.println("Good evening, " + " what’s up?" + " I hope you are good.");

To which the output will be

Good evening, what’s up? I hope you are good.

To concatenate strings and variables, this is the code to devise:

int score = 63;
System.out.println("The results are" + score + " in the previous test.");

So, this method concatenates three different entities: the variable score with “The results are ” and ” in the previous test.” The output will be

The results are 63 in the previous test.

Lastly, a string can be concatenated with mathematical expressions in the manner depicted below.

System.out.println("When you add 70 and 3, we get " + (70 + 3) + ".");

The result is

When you add 70 and 3, we get 73.

In the above example, the mathematical expression was enclosed within parenthesis. The compiler is being instructed to first compute the mathematical expression before concatenating the three entities. This method is ideal for concatenating strings with mathematical expressions and avoiding unforeseen errors.  

Leave a Reply

%d bloggers like this: