Home   |   About   |   Terms   |   Contact    
Read & Learn
 

Increment a variable by a given amount in Java

Java

☯ All Java Articles     ☯ All Articles


A new product.






Popular Google Pages:




This article is regarding How to increment a variable by a given amount in Java?
Last updated on: .



◕ How to increment a variable by a given amount in Java?

Let have an example first:
numBalls = numBalls + 1;

In the above assignment statement we add 1 to the variable numBalls and stored the result value again on the same variable numBalls.
As a result, the new value of the variable numBalls will increase by 1.
This is a very common practice in a program.



◕ By replacing the 1 with 5 we can increment the value of the variable numBalls by 5.
Example:
numBalls = numBalls + 5;

This will increase the value of numBalls by 5.



◕ Same as the addition we can use the subtraction, multiplication as shown bellow.

■ subtraction:
numBalls = numBalls - 1;

■ Multiplication:
numBalls = numBalls * 2;



Related Java articles:
What is Java? Where we can apply Java?
Why Java is a Machine Independent Program? What is Machine Independent Program?
Root Directory of Java
How to Compile and run a Java Program?
What is .class file in Java?
What is variable in a program?
What is identifier in Java?
Integer data types in Java?
What is Primitive Types in Java?
How to declare constant variables in Java?
Why should we declare the initial value of a variable in Java?
Use of Underscores to write an integer in Java





Popular Google Pages:





Top of the page

Amazon & Flipkart Special Products

   


Top of the page