Lesson 8: Addition in Java

In the last chapter, we were introduced to how to use the addition symbol + to join sentences together.  In this lesson, we are going to go over how to add numbers together in Java. 

There are several different ways we can go about adding numbers together in Java.  One method is to add numbers directly inside of our print method:

Or we can reference our variables to add them as shown below:

Using either technique shown above produces the same result: 6.  However, what if we want to make an entire new number value from our addition and store it as a variable? 

First let’s make a class and implement the main method.  Let us make the class name Addition.  Then we can declare 2 int variables: int a and int b.  We can make int a = 6 and int b = 4

Now, in order to store our sum variable in Java, we will make a third variable named int c.  Instead of assigning a number to it, we will make the value of c equal to the addition of a and b.

After making the value of int c equal to a + b, use the print method to print c.

The code above should print out the number 10.

Nice work!  In the next lesson we will go over multiplication.

GitHub link.

Leave a Reply

%d bloggers like this: