In this blog post, you will learn how to carry out arithmetic calculations in Java.
Arithmetic Operators
Java supports the same four basic arithmetic operations as a calculator—addition, subtraction, multiplication, and division.
The combination of variables, literals, operators, and/or method calls is called an expression. For example, (a + b) / 2 is an expression.
Parentheses are used just as in algebra: to indicate in which order the parts of the expression should be computed.
If you mix integer and floating-point values in an arithmetic expression, the result is a floating-point value. For example, 7 + 4.0 is the floating-point value 11.0
Increment and Decrement operators
Changing a variable by adding or subtracting 1 is so common that there is a special shorthand for it.
++ : increment operator, increments variable value by 1.
-- : decrement operator, decrements variable value by 1.
a++; // this is called post increment operation. The variable value is incremented after it is being evaluated.
++a; // this is called pre inrement operation. The variable value is incremented before it is used.
Same is case with decrement operator.
Integer division and remainder
If both numbers are integers, then the result of the division is always an integer, with the remainder discarded.
Ex: 7/4 evaluates to 1, because 7 divided by 4 is 1 with a remainder 3. To get correct answer, make one of the number floating point number, like 7.0/4 will give exact result.
You still want to use integers only and interested in remainder, then you % operator. (modulus operator)
The % operator computes the remainder of an integer division.
Powers and Roots
In Java, there are no symbols for powers or roots. To compute them, you must call methods.
All mathematical related functions are part of 'Math' package in Java.
Math.sqrt(x) - to calculate square root
Math.pow(x, y) - to calculate x to the power y
Math.round(x) - Closest integer to x(as a long). Math.round(100.65) returns 101.
Math.abs(x) - absolute value of x. Math.abs(-4) returns 4
Math.max(x, y) - The larger of x and y
Math.min(x, y) - The smaller of x and y
Converting floating point numbers to Integers
Occasionally, you have a value of type double and that you need to convert to the type int. It is an error to assign a floating-point number to an integer.
Ex:
double balance = total + tax;
int dollars = balance; // Error: Cannot assign double to an int
You use a cast(typeName) to convert a value to a different type.
In the above example, you use like this:
int dollars = (int)balance;
Note: Good practice:
When applying the cast operator to an arithmetic expression, you need to place the expression inside parentheses.
Ex: int dollars = (int) (total + tax);
The other way is, If you want to round a floating-point number to the nearest whole number, use the Math.round method. This method returns long integer.
Type cast or type conversions
Assigning the value of a variable to another variable involves conversion.
If both variables are of same type - assignment will succeed. This is identity conversion.
Widening conversion - implicit conversion. Target type is larger than the first type.
int a = 2;
long x = a; // int to long, widening conversion
Narrowing conversion - target type is smaller than the first type. This case, explicit conversion should be specified.
long a = 10;
int b = (int) a; // narrowing conversion.
Assigning the value of a variable to another variable involves conversion.
If both variables are of same type - assignment will succeed. This is identity conversion.
Widening conversion - implicit conversion. Target type is larger than the first type.
int a = 2;
long x = a; // int to long, widening conversion
Narrowing conversion - target type is smaller than the first type. This case, explicit conversion should be specified.
long a = 10;
int b = (int) a; // narrowing conversion.
No comments:
Post a Comment