FAQ: Learn Java: Manipulating Variables - Addition and Subtraction

This community-built FAQ covers the “Addition and Subtraction” exercise from the lesson “Learn Java: Manipulating Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Addition and Subtraction

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can someone provide a better example of how I would use increment and decrement operators?

I tried to play around with the increment operator ++ on the practice, and wasn’t able to get it to work. I received this error:

PlusAndMinus.java:3: error: ‘;’ expected
int zebrasInZoo++ = 8;

For placing it behind the zebrasInZoo variable. The example provided in the lesson made it seem like this was all I had to do. I tried it in other places, behind numerical variables, and tried adding semicolons but these attempts also resulted in errors.

++ increases the value of the operand by 1 and -- decreases the value of the operand by 1. These two operators can only increase or decrease a value by 1 and not any other value. ++ and -- are unary operators, meaning that they only have one operand (the number to be added or subtracted from).

Example

int x = 5;
x++;
System.out.println(x); // prints 6

The reason why the error message you received indicates you are missing a semicolon is because ++ and -- should only have one operand. Java doesn’t expect more code to come after ++ or -- because of this, as it expects the end of the statement.


If you wanted to add 8 to a value (or add any other number), you can use +=, which adds the value on the right side to the value of an existing variable on the left side. Similarly, you can use -= to decrease a value by a certain amount.

Example

int y = 10;
int z = 2;
y += 5;
z -= 2;
System.out.println(y); // prints 15
System.out.println(z); // prints 0
2 Likes

Can we add double value to int value. If so, which datatype do we get?

zebrasInZoo - 2; is not a statement, what do i do?

A bit late but maybe it helps others:

zebrasInZoo = zebrasInZoo - 2;

or you can use the short form which does the same:

zebrasInZoo -= 2