JAVA FOUNDATIONS · JAVA 17
Java variables and operators: totals without truncation
Choose numeric types deliberately, understand integer division, and calculate a small invoice using whole paise.
Forsk Coding School learning resources · Updated 2026-10-26
What you will learn
- Distinguish an integer from a decimal calculation
- Trace assignment and arithmetic
- Use explicit units in variable names
Before you start: You can compile and run a class with a main method.
A number needs both a type and a meaning
A variable names a value that a program can read or change. Its type determines which values and operations are available. An int can represent a whole count such as seats or units. A double represents floating-point values and is useful for approximate measurements. Neither the type name nor the compiler tells you whether a number means rupees, paise or minutes; choose a variable name that records the unit.
The invoice below uses long values for whole paise. This keeps the small example in integer arithmetic. It is a learning technique rather than a complete payment system: real applications also need explicit rounding rules, validation, appropriate bounds and often BigDecimal for decimal financial calculations. Do not silently mix amounts expressed in rupees and paise.
Understand which calculation happens first
Multiplication and division bind more tightly than addition and subtraction. Parentheses make the intended grouping explicit. The expression 7 / 2 evaluates to 3 because both operands are integers; it does not first produce 3.5 and store that value. By contrast 7 / 2.0 uses floating-point arithmetic and produces 3.5.
Assignment stores a result in the variable on the left. Equality testing uses ==, while = performs assignment. Compound assignment such as total += amount updates an existing variable. When investigating a wrong result, write down the type and unit of each intermediate value before adding more operators to the expression.
Make boundary conditions part of the design
An integer type has a finite range. A wider type can help with an expected larger range, but it does not make arithmetic unlimited. This example uses small positive constants and deliberately avoids overflow. For production calculations, define the largest valid quantity and amount, check incoming values and decide how an overflow must be reported.
Try the same calculation with zero quantity, then one unit, then several units. Ask whether negative quantities should be rejected or represent a return. That is a business rule you must choose explicitly. Tests are more useful when they check those decisions than when they merely repeat one successful calculation.
Run the worked example
Save as Main.java. Run javac Main.java, then java Main.
public class Main {
public static void main(String[] args) {
long unitPaise = 12550;
int quantity = 3;
long totalPaise = unitPaise * quantity;
System.out.println(totalPaise);
System.out.println(7 / 2);
System.out.println(7 / 2.0);
}
}Expected result
37650
3
3.5- The suffix-free literal 12550 fits in an int and is widened when assigned to long.
- Multiplication combines the long unit price with the integer quantity.
- Changing one division operand to 2.0 changes the arithmetic used in that expression.
Common mistakes to check
- Assigning 7 / 2 to a double still stores 3.0; the division already used integers.
- Write long constants with an L suffix when they exceed the int literal range.
- Give quantities and amounts separate names so an incorrect unit is visible.
Try it yourself
Calculate the total for four notebooks costing 2375 paise each. Then print the average of marks 7 and 8 as a decimal value.
Give me a hint
Multiply the whole-paise price by quantity. Divide the sum of marks by 2.0.
Compare with one working solution
public class Main {
public static void main(String[] args) {
long unitPaise = 2375L;
System.out.println(unitPaise * 4);
System.out.println((7 + 8) / 2.0);
}
}Expected result
9500
7.5Quick check
Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.
Keep learning
Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.
Explore the practice workbookReference documentation
Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.
