Author Topic:   integer literals
Rolf Weasel
ranch hand
posted February 27, 2000 03:47 PM             
floating point literals default to type double. do integers default to any one of the integer types?

Paul Wheaton
sheriff
posted February 27, 2000 04:51 PM             
They default to "int"

Rolf Weasel
ranch hand
posted February 27, 2000 07:27 PM             
What if they are >Integer.MAX_VALUE?

Tony Alicea
sheriff
posted February 27, 2000 08:43 PM             
Then you need to use type long

maha anna
bartender
posted February 27, 2000 09:35 PM             
Tony!
What do you mean? Integer.MAX_VALUE is also an int right? Integer.MAX_VALUE defaults to primitive int.
regds
maha anna

Jim Yingst
sheriff
posted February 27, 2000 10:09 PM             
Yes - but anything larger than Integer.MAX_VALUE can't fit in an int, so it has to be long. (See the little ">" symbol in Rolf's post? That meant "larger than Integer.MAX_VALUE".)

maha anna
bartender
posted February 28, 2000 08:33 AM             
yes Jim. I think I am reading toooooo fast.
maha anna

Rolf Weasel
ranch hand
posted February 29, 2000 08:52 PM             
paul, how can integer literals default to int? that would mean :
short s = 1;
would cause a compile time error without an explicit cast.

Jim Yingst
sheriff
posted February 29, 2000 09:56 PM             
That's a special case. From the JLS section on assignment conversion:

In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:

  • The expression is a constant expression of type int.
  • The type of the variable is byte, short, or char.
  • The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable.

That's the case here, so the compiler performs a narrowing conversion without a cast.

To see that the default is int, try this:

public class Test {
static void method(byte b) {
System.out.println(b + " is a byte!");
}
static void method(short s) {
System.out.println(s + " is a short!");
}
static void method(int i) {
System.out.println(i + " is an int!");
}
static void method(long l) {
System.out.println(l + " is a long!");
}
public static void main(String[] args) {
method(1);
}
}

Rolf Weasel
ranch hand
posted February 29, 2000 10:14 PM             
Thanks, Jim!
I ran the code and u were right.
I also tried the following:
public class Test {
static void method(byte b) {
System.out.println(b + " is a byte!");
}
static void method(short s) {
System.out.println(s + " is a short!");
}
static void method(int i) {
System.out.println(i + " is an int!");
}
static void method(long l) {
System.out.println(l + " is a long!");
}
public static void main(String[] args) {
method( Integer.MAX_VALUE+1 );
}
}
and i got :
"-2147483648 is an int!"
should'nt Integer.MAX_VALUE+1 default to long?

Rolf Weasel
ranch hand
posted February 29, 2000 10:23 PM             
that's funny. i just tried:
public static void main(String[] args) {
long l = 2147483648;
method( l );
}
and i got a compile time error saying "Integer literal out of range."
So it looks like all integer literals appearing in code must be in the range -2147483648 to 2147483647.

Rolf Weasel
ranch hand
posted February 29, 2000 10:25 PM             
even an explicit cast did'nt work:
public static void main(String[] args) {
long l = (long) 2147483648;
method( l );
}

Tony Alicea
sheriff
posted February 29, 2000 10:26 PM             
No; it makes the number "roll over" making the most significant bit a ONE, therefore a negative number.

Rolf Weasel
ranch hand
posted February 29, 2000 10:35 PM             
Coming back to my earlier question, how do u specify a literal of value >Integer.MAX_VALUE?

Rolf Weasel
ranch hand
posted February 29, 2000 11:51 PM             
Got the answer in the JLS. gotta use 2147483648L. Man, i clean forgot about L!

justajoke
unregistered
posted March 01, 2000 07:40 AM           
Is Rolf Weasel discussing with himself?

|