Java. Operations. Arithmetic operations




Operations. Arithmetic operations



Search other websites:

1. What groups of operations are supported by Java?

The Java programming language supports a wide range of operations. Operations are used in expressions.

There are 5 groups of operations:

  • arithmetic operations;
  • bitwise logical operations (~, &, |, etc.);
  • logical operations (&, |, &&, ||, etc.);
  • comparison operations (==, !=, >, <, etc.);
  • additional operations for special cases.

 



2. What are the features of the use of arithmetic operations?

Arithmetic operations are used in mathematical expressions.

Arithmetic operations are similar to operations of algebra.

Arithmetic operations must be of a numeric type. Detailed description of the types of data can be found here.

Arithmetic operations can also be performed for the type ‘char‘.

Do not perform arithmetic operations on logical data types (boolean).

Java supports the following arithmetic operations:

Operation      Description
------------------------------------------------
   +           Addition (and unary plus)
   -           Subtraction (also unary minus)
   *           Multiplication
   /           Division
   %           Remainder of division
   ++          Increment (прирост на 1)
   --          Decrement (прирост на 1)
   +=          Addition with assignment
   -=          Subtraction with assignment
   *=          Multiplication with assignment
   /=          Division with assignment
   %=          Remainder of dividing with assignment

 

3. An example of using the arithmetic operations +, , *, /
// arithmetic operations '+', '-', '*', '/'
// integer types
int a,b;
long l;

a = 8;
b = -5;

l = a + b; // l = 3
l = a - b; // l = 13
l = a * b; // l = -40
l = a / b; // l = -1 - evenly divisible division

// real types
float f1, f2;
double d;

f1 = 3.5f;
f2 = 10.0f;
d = f1 + f2; // d = 13.5
d = f1 - f2; // d = -6.5
d = f1 * f2; // d = 35.0
d = f1 / f2; // d = 0.3499999940395355

// real and integer types
a = 5;
f1 = 3.2f;
d = a * f1; // d = 16.0
d = a + f1; // d = 8.199999809265137
d = a - f1; // d = 1.7999999523162842
d = a / f1; // d = 1.5625

// the 'char' type
char c = 'A';
c = (char)(c + 1); // c = 'B'
int code;
code = (int)(c + 1); // code = 67

 

4. An example of using the remainder of division %

Operation % returns the remainder of the division. This operation can be applied:

  • to the integer types (byte, short, int, long);
  • to the real types (float, double).

For example, if the 8 divide by 5, then the remainder of the division is equal to 3.

// % - remainder of division
// integer types
byte b;
b = 8 % 5; // b = 3
b = 8 % 4; // b = 0

int a;
a = 15;
b = (byte)(8 % a); // b = 8

// real types
float f1, f2;
double d;

f1 = 3.5f;
f2 = 10.0f;
d = f1 % f2; // d = 3.5

f2 = 0.5f;
d = f1 % f2; // d = 0.0

f2 = 1.2f;
d = f1 % f2; // d = 1.1

// integer and real types
int x = 5;
float ff = 3.5f;
double dd;
dd = x % ff; // dd = 1.5

 

5. An example of using of increment ++ and decrement – –

Operation increment ‘++‘ increments the operand by 1.

Operation decrement ‘‘ decreases the value of the operand by 1.

// increment '++', decrement '--'
// integer types
long x = 9;
x++; // x = 10

x = -15;
x--; // x = -16

// real types
float f;
f = 2.3f;
f++; // f = 3.3

f = -3.85f;
f--; // f = -4.85

The operation of the increment

a++;

can be replaced

a = a + 1;

Also, the operation of the decrement

a--;

can be replaced

a = a - 1;

 

6. What is the difference between the prefix form and postfix form of the increment (++) and decrement (– –) operations

The difference between the prefix and postfix forms is manifested in cases where these operations are the part of a more complex expression.

In the prefix form (++a or –a) the sequence of operations is as follows:

  • is increased (decreased) the value of the operand a by 1;
  • the resulting value of the operand a is used in the expression.

In the postfix form (a++ or a–) the sequence of operations is as follows:

  • the previous value of operand a is used in the expression;
  • is increased (decreased) the value of the operand a by 1.

Example.

// increment '++', decrement '--'
// integer types
int a, b;
a = -6;
b = a++; // b = -6; a = -5

a = -6;
b = ++a; // b = -5; a = -5

a = 3;
b = a--; // b = 3; a = 2

a = 3;
b = --a; // b = 2; a = 2

// real types
float f;
double d;
f = 5.7f;
d = ++f; // d = 6.7; f = 6.7

In the given example, the string:

b = ++a;

means that:

  • at first, is increased the value of the operand a;
  • then, the resulting value is assigned to the variable b.

String

b = a++;

means that:

  • at first, the value a is assigned to the variable b;
  • then, the value of a is incremented by 1.





 

7. Examples of using of composite arithmetic operations with assignment +=, -=, *=, /=, %=

Compound arithmetic operations with assignment have 2 advantages:

  • reduce the amount of the entered code, if there are long variable names;
  • implementation of composite arithmetic operations in the Java environment, is more effective than the implementation of corresponding the long operations of assignment.

General view of the composit operation with assignment:

variable operation = expression;

This operation replaces the standard form of assigning:

variable = variable operation expression;

Example. Using of compound statements in the program.

// integer types
int a;

a = 7;
a += 8; // a = a + 8 = 15

a = -3;
a -=5; // a = a - 5 = -3 - 5 = -8

a = 10;
a *= 3; // a = 30

a = 20;
a /= 5; // a = 4

a = 30;
a %= 7; // a = 30 % 7 = 2

// real types
float f;
f = 3.5f;
f += 2*f; // f = f + 2*3.5 = 10.5
f = -8.0f;

f -= f;  // f = f - f = 0.0
f = 6.5f;

f %= 2; // f = 0.5

 


Related topics