aspose file tools*
The moose likes Java in General and the fly likes curiosity regarding int ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "curiosity regarding int ?" Watch "curiosity regarding int ?" New topic
Author

curiosity regarding int ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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 ?


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
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

Joined: Mar 17, 2011
Posts: 7676
    
  19

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

Joined: Jul 03, 2011
Posts: 692

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4366
    
    8

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

Joined: Jul 03, 2011
Posts: 692

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

Joined: Mar 17, 2011
Posts: 7676
    
  19

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
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
I am quite sure you can get a ten‑digit int literal in NetBeans. Provided it is not > 2147483648.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7676
    
  19

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
Sheriff

Joined: Oct 13, 2005
Posts: 38405
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: curiosity regarding int ?