Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Some Java Arithmetic

Working with numbers in Java programs.

 

 

 

 

 

 

doubles are real numbers (with decimal places)

 

 

 

 

 

 

 

 

The increment, decrement and combined operators are not part of JETS (IB's Java subset) so won't be used in examination questions, although you can use them in your answers if you wish.

 

 

 

 

 

 

 

 

Very big is about ±9 x 10E12, in general, the integers are used when greater precision is required and whole numbers can be used, floating point numbers have a greater range but store the number values with some (small) approximations.

It's not a good idea to mix floating-point (real) numbers and integers when doing calculations.

 

 

 

 

 

The simple arithmetic operators are:

symbol operator
+
add
-
subtract
*
multiply
/
divide

All of them can be used with doubles and integers to do arithmetic. However, when divide is used with int primitives an int result is given. For example, the following code:

      int x = 23;
    int y = 3;
    int z = x / y;    
// z will have the value 7

Results of a calculation are assigned to a primitive using the assignment operator (=):

     int total = number1 + number2;

The assignment statement is not an equals sign since we can use it to assign an increment to an existing variable:

     total = total + 1;

You may also come across the increment and decrement operators:

        total++;
      --count;

We are going to avoid them as far as possible since they lead to code that is harder to read, especially when code is written by hand - as in examinations. Equally well, you may sometimes see the assignment and arithmetic operators combined:

    total += 3;
     total = total + 3;    // the same but easier to read

Note also, the + sign can be used to join strings together (concatenate):

    s1 = "Richard M. "
     s2 = "Nixon";
     s3 = s1 + s2;    // s3 contains "Richard M. Nixon"

Back to top

Primitive Types

Primitive Storage range of values Other features
byte 1 byte 128 to +127 whole numbers only
char 1 byte all characters characters
int 4 bytes -2147483648 to 2147483647 whole numbers only
long 8 bytes -very big to + very big whole numbers only
boolean 1 byte true or false Only has these two values
double 8 bytes about ±4.9E-324 to ±1.79E+308 real numbers
short 2 bytes -32768 to +32767 whole numbers only
float 4 bytes about ±1.4 E-45 to ±3.4E+38 real numbers

We won't be using short and float on the course. They are also not included as part of JETS.

String types, which we have already met, are collections of characters. In some ways they are like primitives since you can assign a value to a String directly. However, String is a Class and comes with a lot of useful methods which we will meet along the way. Primitives don't have methods.

Back to top

Some exercises to try:

Multiplier
Change the Applet to multiply two numbers together (easy); how about divide (any unexpected results when dividing integers?).

Convertor
Make an Applet that converts one quantity to another (like dollars to pounds or pounds to kilos). You can try using Labels to indicate what you expect the user to type into the TextFields. You might want to try Panels to improve layout (see the how to above).

Related: [ Java Home | previous:AddMe.java | next:more about classes & methods ]

Might be worth bookmarking this page as a useful reference for early problems and exercises.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008