|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Easy Learn Java: Programming Articles, Examples and Tips - Page 366
Previous
1060 Stories (530 Pages, 2 Per Page)
Next
Integers
|
Java: Integers
Integers are whole numbers, for example, -35, 0, 2048, .... Integers are
represented in binary inside the computer, and in decimal in Java source
programs. Java automatically converts decimal numbers you write in your source
program into binary numbers internally.
Four kinds of integers
The are four types of integers in Java: byte, short,
int, long. The most common is int.
All integers are stored in signed, two's-complement, format.
Technically, char is an unsigned integer type altho it is used to
store characters. This is largely because of Java's legacy from C++. Don't use
char for integers unless you are sure of what you're doing.
How Java stores integers in memory
Java stores all integers in memory as binary numbers.
| type |
Size |
Range |
| name |
bytes |
bits |
minimum |
maximum |
| byte |
1 |
8 |
-128 |
+127 |
| short |
2 |
16 |
-32,768 |
+32,767 |
| int |
4 |
32 |
-2,147,483,648 |
+2,147,483,647 |
| long |
8 |
64 |
-9,223,372,036,854,775,808 |
+9,223,372,036,854,775,807 |
How to write integer literals
Here is how to write decimal integer literals (constants).
int literals are written in the usual decimal notation,
like 34 or -222.
long literals are written by adding an L (or lowercase l
altho this is almost impossible to distinguish from the digit 1), eg, 34L or
-222L.
- There is no way to write a literal
byte or short,
altho sometimes Java will automatically cast an int literal to the
appropriate type.
Hexadecimal literals
You can write an int in hexadecimal by prefixing the hexadecimal number with the
digit zero followed by the letter x, "0x" or "0X". The hexadecimal digits are
0-9 and the letters a-f in upper- or lowercase.
int i;
i = 0x2A; // assigns decimal 42 to i.
Errors
Operations may produce numbers which are too large (overflow) to be stored in an
int. No error is caused in this case; the result is simply an
incorrect number (one of the shames of modern computer arithmetic). Division by
zero will cause an execution exception (ArithmeticException).
3 comments | | Score: 0
|
Posted by jalex on Tuesday, May 31, 2005 (00:00:00) (2530 reads)
|
Floating-point
|
Floating-point numbers are like real numbers in mathematics, for
example, 3.14159, -0.000001. Java has two kinds of floating-point numbers:
float and double, both stored in IEEE-754 format. The
default type when you write a floating-point literal is double.
Java floating-point types
| type |
Size |
Range |
Precision |
| name |
bytes |
bits |
approximate |
in decimal digits |
| float |
4 |
32 |
+/- 3.4 * 1038 |
6-7 |
| double |
8 |
64 |
+/- 1.8 * 10308 |
15 |
Limited precision
Because there are only a limited number of bits in each floating-point type,
some numbers are inexact, just as the decimal system can not represent some
numbers exactly, for example 1/3. The most troublesome of these is that 1/10 can
not be represented exactly in binary.
Floating-point literals
There are two types of notation for floating-point numbers. Any of these
numbers can be followed by "F" (or "f") to make it a float instead
of the default double.
- Standard (American) notation which is a series of digits for the
integer part followed by a decimal point followed by a series of digits for
the fraction part. Eg, 3.14159 is a
double. A sign (+ or -) may
precede the number.
- Scientific notation which is a standard floating-point literal
followed by the letter "E" (or "e") followed by an optionally signed
exponent of 10 which is used as a multiplier (ie, how to shift the decimal
point). Generally scientific notation is used only for very large or small
numbers.
| Scientific |
Standard |
| 1.2345e5 |
123450.0 |
| 1.2345e+5 |
123450.0 |
| 1.2345e-5 |
0.000012345 |
Infinity and NaN
No exceptions are generated by floating-point operations. Instead of an
interruption in execution, the result of an operation may be positive infinity,
negative infinity, or NaN (not a number). Division by zero or overflow produce
infinity. Subtracting two infinities produces a NaN. Use methods in the wrapper
classes (Float or Double) to test for these values.
References
6 comments | | Score: 0
|
Posted by jalex on Monday, May 30, 2005 (00:00:00) (2437 reads)
|
|
|