|
|
||
| You are here: | ||
|
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:
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; Results of a calculation are assigned to a primitive using the assignment operator (=): int total = number1 + number2; total = total + 1; You may also come across the increment and decrement operators: total++; 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; Note also, the + sign can be used to join strings together (concatenate): s1 = "Richard M. "
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. Some exercises to try: Multiplier Convertor 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. |
|
|
|||
|
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. 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 |