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
1060 Stories (530 Pages, 2 Per Page)
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:
long. The most common is int.
All integers are stored in signed, two's-complement, format.
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.
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
- There is no way to write a literal
altho sometimes Java will automatically cast an int literal to the
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.
i = 0x2A; // assigns decimal 42 to i.
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 numbers are like real numbers in mathematics, for
example, 3.14159, -0.000001. Java has two kinds of floating-point numbers:
double, both stored in IEEE-754 format. The
default type when you write a floating-point literal is
Java floating-point types
||in decimal digits
||+/- 3.4 * 1038
||+/- 1.8 * 10308
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.
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
of the default
- 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
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.
6 comments | | Score: 0
Posted by jalex on Monday, May 30, 2005 (00:00:00) (2437 reads)