constant of type long

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
 
A

Andrew Thompson

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 variant of your code..
<sscce>
class TestLong {

public static final long MAX = 0x00000000FFFFFFFFl;

public static final void main(String[] args) {

try {
System.out.println(MAX);

long bigLong = 4294967295l;
System.out.println( );

System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}

}

}
</sscce>
 
A

Andrew Thompson

<sscce>
class TestLong {

public static final long MAX = 0x00000000FFFFFFFFl;

public static final void main(String[] args) {

try {
System.out.println(MAX);

long bigLong = 4294967295l;
System.out.println( );

Ooops!
System.out.println( bigLong );
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
}

}

}
</sscce>

There..
 
B

Bjorn Abelli

"harald ebert" ...
can anybody tell me
- why the long value MAX is interpreted as integer
(see program output)

All numeric, non-decimal *literals* are interpreted as int:s, unless you
suffix them.
public static final long MAX = 0x00000000FFFFFFFF;

....should be:

public static final long MAX = 0x00000000FFFFFFFFL;
---------------------------------------------------^
- why the long constant 4294967295 is rejected
by the compiler? (see compiler output)

See answer above.
// System.out.println((long) 4294967295);

System.out.println((long) 4294967295L);
---------------------------------------^

As an "unsuffixed" numeric, non-decimal literal defaults to the int type,
the value 4294967295 is too big to fit into an int, hence the error message,
as it occurs *before* your try to cast it to a long.

P.S. Questions of this type should rather be sent to
com.lang.java.help.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top