hts.StevenWood.com

[ Last Page ]

Java math fun!

Sometimes math results are slightly different than expected when using Java. This often comes about because Java considers the type of number when doing math operations. The result of a math operation can only be placed in a variable that can contain the result. Remember that variables take up different amounts of memory based on their type. Bytes take up less space than integers take up less space than doubles etc.

It is possible to put an integer math result into a double variable (a bigger box) but trying to do the opposite will cause Java to complain.

Look at this java file: DivisionTest.java

Compile and running the DivisionTest.java file. You should see output similar to this:

You will note the following things:

  • Integer division gives an integer result: NO decimal part
  • Double division gives a double result: decimals included
  • Mixed division is allowed and gives a double result.
  • Putting the result of an integer or double division into a double variable is allowed
  • Putting the result of a double division into an integer variable is not allowed (usually).
    • To do this you must use type-casting
      Tell java to treat a variable of one type as another type.
    • If  a  and b is of type integer and x and y are of type double  then you can do this:

      a = (int) x / (int) y; // treat x and y as integers
      x = (double) a / (double) b; // treat a and b as doubles
       
    • Of course, this works for other types too
 
[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+

Java math fun

hts.StevenWood.com

[ Last Page ]

Java math fun!

Sometimes math results are slightly different than expected when using Java. This often comes about because Java considers the type of number when doing math operations. The result of a math operation can only be placed in a variable that can contain the result. Remember that variables take up different amounts of memory based on their type. Bytes take up less space than integers take up less space than doubles etc.

It is possible to put an integer math result into a double variable (a bigger box) but trying to do the opposite will cause Java to complain.

Look at this java file: DivisionTest.java

Compile and running the DivisionTest.java file. You should see output similar to this:

You will note the following things:

  • Integer division gives an integer result: NO decimal part
  • Double division gives a double result: decimals included
  • Mixed division is allowed and gives a double result.
  • Putting the result of an integer or double division into a double variable is allowed
  • Putting the result of a double division into an integer variable is not allowed (usually).
    • To do this you must use type-casting
      Tell java to treat a variable of one type as another type.
    • If  a  and b is of type integer and x and y are of type double  then you can do this:

      a = (int) x / (int) y; // treat x and y as integers
      x = (double) a / (double) b; // treat a and b as doubles
       
    • Of course, this works for other types too
 
[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+