Popular Google Pages:
This article is regarding How to increment a variable by a given amount in Java?
Last updated on: 27th February 2017.
◕ 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.
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.
numBalls = numBalls - 1;
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
Top of the page