• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

curiosity regarding int ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have checked java's int's max value using

abd when you counts its digit its 10 but when you write or assign a value to a int variable it only allow you to assign 9 digit value , i guess this is to prevent us from arithmetic overflow, does it ?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can write an int literal up to 10 digits. And it does not prevent overflow. You can use any decimal number between 0 and 2147483648 inclusive, and (apart from 0) they are all positive. What can you do with the largest value? I shall let the Java Language Specification answer that.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, you can write an int literal up to 10 digits. And it does not prevent overflow. You can use any decimal number between 0 and 2147483648 inclusive, and (apart from 0) they are all positive.


Actually, you can use any value from -2147483648 to 2147483647, and (apart from 0) slightly more than half of them are negative.

Winston
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That’s what everybody thinks, but if you look in the JLS link I posted earlier, you find the minus sign never forms part of an integer literal. The grammar mentions 0 1 2 3 4 5 6 7 8 9 (and now it’s Java7, _). No - anywhere. What’s more, if you wind down to the bit about floating-point, the only place - appears is after e in a number like 123.45e-67.
When you write -123 you are not writing -123 but the positive number 123 immediately preceded by the sign-change/unary minus operator.

Even more confusingly binary (Java7 only) octal and hex literals are unsigned up to 0xffff_ffff (int) which represents -1decimal. So you can represent the actual bit values in a literal (octal, hex or binary), and (I think) display them converted to hex with the %x tag and printf.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That’s what everybody thinks, but if you look in the JLS link I posted earlier, you find the minus sign never forms part of an integer literal.


Interesting. However, I'm pretty sure I'm right that the upper limit is 2147483647 (although it could possibly be an Eclipse error).

Winston
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, you can write an int literal up to 10 digits. And it does not prevent overflow. You can use any decimal number between 0 and 2147483648 inclusive, and (apart from 0) they are all positive. What can you do with the largest value? I shall let the Java Language Specification answer that.


Soory, but i tried one more time in netbeans and it is actually not allowing me to assign value more than 9 digits to a variable of type int.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:

Campbell Ritchie wrote:No, you can write an int literal up to 10 digits. And it does not prevent overflow. You can use any decimal number between 0 and 2147483648 inclusive, and (apart from 0) they are all positive. What can you do with the largest value? I shall let the Java Language Specification answer that.


Soory, but i tried one more time in netbeans and it is actually not allowing me to assign value more than 9 digits to a variable of type int.



If the int is between Integer.MIN_VALUE and Integer.MAX_VALUE, inclusive, it will let you. There must be something you're not telling us.


 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works in Netbeans when I try it, as long as the 10-digit number is no greater than 2147483647. Which number are you trying to assign?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:It works in Netbeans when I try it, as long as the 10-digit number is no greater than 2147483647. Which number are you trying to assign?


ohh, i got that i was assigning more than 2147483647....thanks guys
but dont you think
int i = 2147483647 + 2147483647 // will cause a overflow right ?
but why doesn't java gives an error for that , why it runs and gives answer as -2 ?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:but dont you think
int i = 2147483647 + 2147483647 // will cause a overflow right ?


What do you think?

but why doesn't java gives an error for that , why it runs and gives answer as -2 ?


You need to read the JLS section on integer arithmetic.

Winston
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . the upper limit is 2147483647 . . .

Winston

The JLS page I quoted earlier says 0...2147483648. But one particular value (2147483648) has to have certain treatment. When you see what the JLS says that particular treatment is, you will think it is obvious.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am quite sure you can get a ten‑digit int literal in NetBeans. Provided it is not > 2147483648.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you missed the line after the one you're talking about. I quote:

The largest decimal literal of type int is 2147483648 (2^31).

All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear.

It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).


So Winston was definitely right; although 2147483648 is decimal literal of type int, 2147483647 is the upper bound for int itself.

I think the problem here is how it's worded. The JLS says that 2147483648 is a decimal literal of type int. It does not say it's an int literal.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:I think the problem here is how it's worded.


Telling me. Four screenfuls of arcane crud just to explain that an integer can be given a value between -2147483648 and 2147483647?

I suppose a compiler writer might need to know about all that 'literal' nonsense, but it seems to me that the JLS authors could have provided a "layman's" explanation as well.

It would also suggest that the use of literals with any type smaller than int is going to involve a narrowing primitive conversion...or possibly a widening one for a comparison. Sheesh.

Winston
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Decimal literal of type int not an int?

Yes, I agree that is worded badly. But the JLS is the formal specification; if you want a layman’s description you have to try here. Where it shows literals in various format, all without minus signs, and tells us ints can be from -2147483648 to 2147483648 inclusive. So that doesn’t help either.
And yes, if you write byte b = 123; that requires a narrowing conversion.
 
reply
    Bookmark Topic Watch Topic
  • New Topic