Assignments & Arithmetic in Java

 

Assignment is one way to get a value into a variable.

 

Java is strongly typed:

·       variables must be declared as to their type before they are used

·       values that go into variables must be of appropriate type.

 

Declarations look like:

    type varName;

    type varName1, varName2, …, varName10;

    type varName = value;

 


Integers vs Real Numbers

 

For numeric quantities, we use integers or real numbers:

 

Integers – byte, short, int, long

 

Real numbers – float, double

        1000000.0  is represented in computer as 1.0 x 106

        100000000.0 is represented as 1.0 x 108

 

        // the decimal point ‘floats’ around

 

Note: most of Java’s built-in Math functions use double arguments and produce double results.


Arithmetic Operators

 

                + addition

-  subtraction

* multiplication

/ division

% remainder of integer division

 

 

Arithmetic results:

·       if both operands are int, so is result

·       if either operand is real, so is result

 

Rules designed to preserve as much info as possible, so if one operand is a double and the other a float, the result will be a double.


Examples:

                        7/2           yields       3

                        7.0/2.0     yields       3.5

                        7.0/2        yields       3.5

                        7 % 2       yields       1

                        7 / 2 / 1.0 yields       3.0

 Multiple operators? Rules of algebra followed:

                        2*3+4       yields       10

                        2+3*4       yields       14

 As in algebra, precedence can be overridden by parentheses:

                        2*(3+4)    yields       14

                        (2+3)*4    yields       20

 

Type promotion does not occur until necessary, so in an expression like:  2 * 3 * 2.0 + 4/3, the following computations are done: 

2*3 yields 6

6 * 2.0 yields 12.0

4/3 yields 1

12.0 + 1 yields 13.0


Assignments – What values stored?

 

        int I;

        double d;

 

        i = 4 * 3;

i = 4 / 3;

i = 3 / 4;

i = 4 % 3;

i = 3 % 4;

d = 4.0 * 3.0;

d = 3.0 / 4.0

d = 4.0 / 3.0;

d = 4 * 3;

d = 3 / 4;

i = 3.0 / 4.0;

                       


            In Java we can assign a value to a variable of a type that is the same size or larger.

                        Data Type                Max value

Integers:

                        byte                                      127

                  short              32767

           int            2147483647

                        long                                      over 1018

Reals:

                        float                                    over 1038

                        double                                 over 10308

 

We can assign a smaller value to a bigger variable.  We can assign:

                        byte to short

                        short to int

                        int to long

                        long to float

                        float to double 


Narrowing – storing a larger value into a smaller variable.

 

Never done automatically – attempts generate compile-time errors.

 

Programmer can explicitly have values narrowed by using a casting operation:

 

double x = 1000000;

byte i;

 

i = (byte) x;   // stores 127 in i – stores closest approximation

 

x = 3 / 4 * 2.0; // result is 0.0

 

x = (double) 3 / 4 * 2.0;  // result is 1.5

Java’s Math class

Available automatically from any Java program.  

Useful functions in Math:

 

    abs(x)     absolute value (int or double)

    ceil(x)    smallest int >= x (double)

    floor(x)   largest int <= x (double)

    round(x)   rounds x (int or double)

    max(x,y)   larger (int or double)

    min(x,y)   smaller (int or double)

    pow(x,y)   x raised to the y (double)

    sqrt(x)    square root (double)

    random()   between 0.0 and 1.0 (double)