H
harald ebert
Hello,
can anybody tell me
- why the long value MAX is interpreted as integer (see program
output)
- why the long constant 4294967295 is rejected by the compiler? (see
compiler output)
# my code:
class Test {
public static final long MAX = 0x00000000FFFFFFFF;
public static final void main(String[] args) {
try {
System.out.println(MAX);
// System.out.println((long) 4294967295);
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
# program output:
c:\Tool\Common\AIDA\studio\jre\bin>java Test
-1
# after uncommenting the line with the number constant and recompiling
# compiler output:
c:\Tool\Common\AIDA\studio\jre\bin>javac Test.java
test.java:9: integer number too large: 4294967295
System.out.println((long) 4294967295);
^
1 error
# I found the following in the Java Language Specification on the SUN
page
4.2.1 Integral Types and Values
The values of the integral types are integers in the following
ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
--> For long, from -9223372036854775808 to 9223372036854775807,
inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0
to 65535
# used Java version
c:\Tool\Common\AIDA\studio\jre\bin>java -version
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)
can anybody tell me how to define the constant without compiler error
Thanks for your help
Harald Ebert
can anybody tell me
- why the long value MAX is interpreted as integer (see program
output)
- why the long constant 4294967295 is rejected by the compiler? (see
compiler output)
# my code:
class Test {
public static final long MAX = 0x00000000FFFFFFFF;
public static final void main(String[] args) {
try {
System.out.println(MAX);
// System.out.println((long) 4294967295);
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
# program output:
c:\Tool\Common\AIDA\studio\jre\bin>java Test
-1
# after uncommenting the line with the number constant and recompiling
# compiler output:
c:\Tool\Common\AIDA\studio\jre\bin>javac Test.java
test.java:9: integer number too large: 4294967295
System.out.println((long) 4294967295);
^
1 error
# I found the following in the Java Language Specification on the SUN
page
4.2.1 Integral Types and Values
The values of the integral types are integers in the following
ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
--> For long, from -9223372036854775808 to 9223372036854775807,
inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0
to 65535
# used Java version
c:\Tool\Common\AIDA\studio\jre\bin>java -version
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)
can anybody tell me how to define the constant without compiler error
Thanks for your help
Harald Ebert