Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If Statement Limitation?  RSS feed

 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the limitation of using the if statement?
long longMaxValue;


if(longMaxValue >=9223372036854775807)
{
}

This would flag an error, stating that "integer number is too large" when the longMaxValue is declared type long.

But if I write it this way, it wouldn't flag an error:
if(longMaxValue >= Long.MAX_VALUE)
{
}

Why?

[ July 17, 2005: Message edited by: Karen Baog ]

[ July 17, 2005: Message edited by: Karen Baog ]
[ July 17, 2005: Message edited by: Karen Baog ]
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karen,

You should postfix 'L' to the number to indicate that it's a long constant. I think by default, all numeric constants are taken by java as int unless otherwise specified. This works:



_steve.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two numbers you posted are the same - the character at the end of the first one is a lowercase 'L', not a one, used to indicate the value is a long.

But to be absolutely sure of the value, why don't you just do this:
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Simon Joseph Fernandez:

I think by default, all numeric constants are taken by java as int unless otherwise specified.
_steve.


Absolutely right. One of the few things I still remember from SCJP preparation
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!