Going through Lesson 4, we learned about variables, how to name them, and talked about the variable type “String.” This lesson we are going to learn how to print a variable.
Let us start off by creating a class called “PrintingStrings” and implement the Java main method. It should look like this:
Next, we are going to create a
String. We are going to name our String “str” and inside our String we will put “Variables are easy”.
Now that we have the basic components of our program and a String, we are also going to add our Java printing method.
However, instead of using quotations marks and writing inside of the printing method, we are going to simply put the name of our String into the parenthesis of our print method:
Now, we can compile and run our code. You should see the following:
Great job! As shown above, we can see that we can print variables by simply putting a variable’s identifier/name into our print method. Although we have only seen Strings so far, it is important to note that this works for any variable type.
Leave a Reply