Saturday, April 16, 2011

Java Integer Arithmetic For Beginners_4389

Java Integer Arithmetic For Beginners Integer art of computation expressions in Java

In Java, whole number arithmetic is performed using "integer premises types".In Java Data Types, we discussed some of these types—byte, short, int and dilatory—and gave examples of "integer constants". An whole number constant (e.g. 23, 0, -245) is the simplest case in point of an integer expression. However, in the greatest degree of the time, we write integer expressions by combining constants and variables by the following arithmetic operators:

For archetype, suppose we have the following declaration:

int a, b, c;

then the following are all valid expressions:

a + 39

a + b - c * 2

b % 10 //the residue when b is divided by 10

c + (a * 2 + b * 2) / 2

The operators +, - and * quite give the expected results. However, / performs whole number division; if there is any residue, it is thrown away. We pronounce integer division truncates. Thus 19 / 5 gives the value 3; the remainder 4 is discarded.

But what is the value of -19 / 5? The answer in the present life is –3. The rule is that, in Java, whole number division truncates towards zero. Since the rigorous value of –19 5 is –3.8, truncating towards nothing gives –3.

The % operator gives the remains when one integer is divided ~ means of another; for example,

19 % 5 evaluates to 4;

j % 7 gives the leavings when j is divided by 7;

 

You have power to use it to test, for urgent solicitation,Apple Laptop Batteries if a reckon j is even or odd. If j % 2 is 0 at that time j is even; if j % 2 is 1, j is extravagant.

 

Precedence of operators

Java evaluates an arithmetic expression based on the normal precedence of operators: multiplication and class are done before addition and subtraction. We say that multiplication and es trangement have higher precedence than addition and deduction. For example, the expression

5 + 3 * 4

is evaluated ~ the agency of first multiplying 3 by 4 (giving 12) and afterward adding 5 to 12, giving 17 considered in the state of the value of the expression.

As prevalent, we batteires9563dsa can use brackets to validity the evaluation of an expression in the mandate we want. For example,

(5 + 3) * 4

primeval adds 5 and 3 (giving 8), and therefore multiplies 8 by 4, giving 32.

When couple operators which have the same preference appear in an expression, they are evaluated from left to as it should be, unless specified otherwise by brackets. For example,

24 / 4 * 2

is evaluated to the degree that

(24 / 4) * 2

(giving 12) and

12 - 7 + 3

is evaluated of the same kind with

(12 - 7) + 3

giving 8. However,

24 / (4 * 2)

is evaluated with the multiplication done first, giving 3, and

12 - (7 + 3)

is evaluated through the addition done first, giving 2.

In Java, the remainder operator % has the same precedence for the re~on that multiplication (*) and division (/).

In Floating Point Numbers and Expressions, we eclaircize how to work with floating-salient trait numbers. In Java double to int and Other Conversions, we external appearance how to mix integers and floating-character numbers in the same expression.

Exercise: What is printed through the following program? Check your say in reply by typing the program to a toothed Exercise.java. Compile the program and proceed it.

public class Exercise {

public static make null main(String[] args) {

int a = 15;

int b = 24;

System.uncovered.printf("%d %d", b - a + 7, b - (a + 7));

System.on the ~side.printf("%d %d", b - a - 4, b - (a - 4));

System.loudly.printf("%d %d", b % a / 2, b % (a / 2));

System.on the ~side.printf("%d %d", b * a / 2, b * (a / 2));

System.not at home.printf("%d %d", b / 2 * a, b / (2 * a));

}

}

 

 

References

Java Programming – A Beginner's Course

 

Related programming references

Article: Java Programming Tutorials: Java Integer Arithmetic

 

 

Dell Laptop Batteries

No comments: