Lesson 6: Printing Multiple Variables

Now that we know how to print a single variable, we are going to learn how to print multiple variables using one print method. However, lets first talk about another variable type: char.

char (which is short for character) is a variable type that is similar to the String type. The main difference is that it can only a single character. This includes letters, numbers, and punctuation symbols. Also, instead of using quotation marks like we do with Strings, we will use apostrophes ' ' on either side of our char. Look below for a comparison of declaring Strings and chars.

Notice how the String has quotations marks, and the char has apostrophes

Alright, now that we know the variable types String and char, let us see how to print multiple variables!

First, lets make a class named: class PrintingVariables and put the Java main method inside of the class.

Next, let us declare 2 variables and use the String type for one, and a char type for the other. For our String we will name it str and put in the text "My name starts with the letter: "

When declaring our char, we will name the char character and put the letter of your first name.

After that, we will write out our System.out.println() method and end our sentence with a ; (semicolon.) Then, we will put our String and char variable names inside of our print method making sure to join them by the + (addition) symbol.

We can now compile and run our code!

You should get something similar to this. If there is no space between your colon and the char, make sure to add a space in your String!

Great job, you can now print multiple variables using a single print method! The addition symbol will come more into play in future lessons, as it is also used for math equations in Java. However, for the time being just remember that it is used to join variables for our print method.

GitHub link.

Leave a Reply

%d bloggers like this: