4.2.2
Integer Operations
TYPES, VALUES, AND VARIABLES
N
The signed and unsigned shift operators
<<
,
>>
, and
>>>
( 15.18)
N
The bitwise complement operator
~
( 15.14.5)
N
The integer bitwise operators
&
,
|
, and
^
( 15.21.1)
The conditional operator
? :
( 15.24)
The cast operator, which can convert from an integral value to a value of any
specified numeric type ( 5.4, 15.15)
The string concatenation operator
+
( 15.17.1), which, when given a
String
operand and an integral operand, will convert the integral operand to a
String
representing its value in decimal form, and then produce a newly cre
ated
String
that is the concatenation of the two strings
Other useful constructors, methods, and constants are predefined in the classes
Integer
( 20.7),
Long
( 20.8), and
Character
( 20.5).
If an integer operator other than a shift operator has at least one operand of
type
long
, then the operation is carried out using 64 bit precision, and the result
of the numerical operator is of type
long
. If the other operand is not
long
, it is
first widened ( 5.1.2) to type
long
by numeric promotion ( 5.6). Otherwise, the
operation is carried out using 32 bit precision, and the result of the numerical
operator is of type
int
. If either operand is not an
int
, it is first widened to type
int
by numeric promotion.
The built in integer operators do not indicate overflow or underflow in any
way. The only numeric operators that can throw an exception ( 11) are the integer
divide operator
/
( 15.16.2) and the integer remainder operator
%
( 15.16.3),
which throw an
ArithmeticException
if the right hand operand is zero.
The example:
class Test {
public static void main(String[] args) {
int i = 1000000;
System.out.println(i * i);
long l = i;
System.out.println(l * l);
System.out.println(20296 / (l i));
}
}
produces the output:
727379968
1000000000000
and then encounters an
ArithmeticException
in the division by
l i
, because
l i
is zero. The first multiplication is performed in 32 bit precision, whereas the
32
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved