• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

integer literals

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
floating point literals default to type double. do integers default to any one of the integer types?
 
Trailboss
Posts: 24043
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They default to "int"
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if they are >Integer.MAX_VALUE?
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you need to use type <CODE>long</CODE>
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony!
What do you mean? Integer.MAX_VALUE is also an int right? Integer.MAX_VALUE defaults to primitive int.
regds
maha anna
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Jim. I think I am reading toooooo fast.
maha anna
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a special case. From the JLS section on assignment conversion:
<blockquote>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.
  • </blockquote>
    That's the case here, so the compiler performs a narrowing conversion without a cast.
    To see that the default is int, try this:
    <code><pre>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);
    }
    }</pre></code>
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even an explicit cast did'nt work:
public static void main(String[] args) {
long l = (long) 2147483648;
method( l );
}
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No; it makes the number "roll over" making the most significant bit a ONE, therefore a negative number.
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Coming back to my earlier question, how do u specify a literal of value >Integer.MAX_VALUE?
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the answer in the JLS. gotta use 2147483648L. Man, i clean forgot about L!
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is Rolf Weasel discussing with himself?
reply
    Bookmark Topic Watch Topic
  • New Topic