menu


Subscribe to the blog

Social networks

My page on vk.com
My page on Facebook
My page on Google plus
My page on Twitter

Main - Blog - Arithmetic Operators in Java

Programming Date of creation: Views: 620

Arithmetic Operators in Java

lesnih.info


Russia

Arithmetic Operators

In this lesson we will discuss arithmetic operations and operators.

I very often use arithmetic operators in my programming projects, such as addition, subtraction, multiplication, division, and incrementing and decrementing in general I use constantly in program cycles, especially incrementing. And now I'll talk about each operator - in details.

But first we must decide what is the operators. Operators are mathematical commands, string commands, logical commands and comparison commands, such as plus, minus, multiply and divide. Thus, the arithmetic operators in the Java language include addition (+), subtraction (-), multiplication (*), division (/), taking the remainder (%), these operators are actually the same as in other similar programming languages.

These arithmetic operations have the same priorities as in a normal school, if in detail they are divided into two priority groups: in the first group this is (/), (*), (%), and in the second priority group: (+), ( -).

Restrictions

Operators (*) of multiplication and (/) division are performed in a higher priority over all primitive numeric types and char. But as usual, you can not divide by 0, in the same way, and the program will throw an error ArithmeticException

In Java, there are restrictions that apply to all numeric data types, but the easiest way to understand this is by example: the numeric data type byte can be in the range -128 to 127. If we make a command

byte xyz = 125 + 15;

then the system will give us an error, because this type of data exceeds the permissible limits, there is not enough bit capacity in this type of data. In this case, the bit depth is byte = 8.

To understand what it is and generally understand what data is, how they are stored in the computer.

On the storage medium, the data is stored in the binary system 1 or 0. Therefore, all data is converted into this binary code.

Therefore, all data was divided into different types of data, depending on how much space they need in the medium itself. Accordingly, all numeric types of data were separated by bit capacity

 Consider, for example, division. If you divide integer data (that is, without a fractional part) into integer other data, then the division result will also be integer.

byte ab = 12/9; // Answer 1

If the data type is integer then the result will be rounded to an integer.

It is necessary to remember about this - if not competently to plan the future architecture of the project, then

Multiplication can lead to a lack of bit capacity, and division can lead to loss of accuracy.

Division by module

Let us now consider such a method as Division by module. The result of division by modulus is the remainder of division. For example, 13/9 is 1, but we still have a remainder of 4, hence 13% 9 = 4. Often the operands are represented as integer types (the operands in this case are 13 and 9), but sometimes this operator is applied to numbers with a floating point. You should also know the features of this operator when the operands are negative.

what is an operand.

Operand in programming languages is the argument of the operation; data that is processed by the command;

There is a rule on which this operator works:

The operator subtracts the right operand from the left operand, until the result is less than the right operand.

And now consider an example:

15% 4 15-4 (11> 5), then 11-4 (7> 5), then 7-4 (3 <5) => Hence 15% 4 = 3

19% 5 19-5 (14> 5), then 14-5 (9> 5), then 9-4 (4 <5) => Hence 19% 5 = 4

22% 6 22-6 (16> 6), then 16-6 (11> 5), then 10-6 (4 <6) => Therefore 22% 6 = 4

Another important point: the sign of the result (positive or negative) depends entirely on which sign is at the left operand.

Increment and decrement

Increment in Java as well as in many other languages is indicated by a double plus sign ++. Decrement is indicated by two minus signs.

Consider an example

Prefix form:

int a = 5; 

++ a; // a = 6 Increment

--a; // a = 5 decrement

Postfix form:

int a = 5;

a ++; // a = 6 Increment

a--; // a = 5 decrement

 

Simple numeric types. Table

Type

Bit depth

Integer types*

MIN

MAX

byte

8 bit

+

-128

127

short

16 bit

+

-32768

32767

int

32 bit

+

-2147483648

2147483647

long

64 bit

+

-9223372036854775808

9223372036854775807

float

32 bit

 

-3.4E+38

3.4E+38

double

64 bit

 

-1.7E+308

1.7E+308

Integer types *

Java defines four integer data types, such as: byte, short, int, long. These data support both positive and negative values.


Arithmetic Operators in Java
Rating: 5 | Тotal votes: 5 | Please, rate this article




Commets:









    Subscribe to the blog


    Random articles from the blog:


    http request and read the html page Java

    http request and read the html page Java
    Topic: Programming

    Tags: java,http,html read

    Views:131

    Java mail - Send an email from a java application

    Java mail - Send an email from a java application
    Topic: Programming

    Tags: java,email,send email

    Views:368

    Arithmetic Operators in Java

    Arithmetic Operators in Java
    Topic: Programming

    Tags: java

    Views:620