This discussion is archived
11 Replies Latest reply: Jan 19, 2007 2:23 AM by 807599

# comparing long to int

Currently Being Moderated
Is this legal as it is not throwing an error:

long a = 1
int b = 1

if a == b ...

Thanks
• ###### 1. Re: comparing long to int
Currently Being Moderated
Is this legal as it is not throwing an error:

long a = 1
int b = 1

if a == b ...

Thanks
I haven't seen the term thrown with errors?
• ###### 2. Re: comparing long to int
Currently Being Moderated
yes thats fine. Both a long and int can hold the same values up to a point, so its ok to check for equality.

Ted.
• ###### 3. Re: comparing long to int
Currently Being Moderated
Is this legal as it is not throwing an error:
The int will be converted to a long before the compare is made. In principle a cast will take place, like

if (a == (long)b)
• ###### 4. Re: comparing long to int
Currently Being Moderated
Is this legal as it is not throwing an error:
The int will be converted to a long before the
compare is made. In principle a cast will take place,
like

if (a == (long)b)
do we need to explicitly cast as you have done above or will it be implicitly handled by java
• ###### 5. Re: comparing long to int
Currently Being Moderated
do we need to explicitly cast as you have done above
or will it be implicitly handled by java
You don't have to cast.

But why aren't you testing it?

Kaj
• ###### 6. Re: comparing long to int
Currently Being Moderated
Thanks I will test now. Also can you please tell me how I convert a Long to an Integer?

Thanks

vik
• ###### 7. Re: comparing long to int
Currently Being Moderated
do we need to explicitly cast as you have done above
or will it be implicitly handled by java
Java will cast primative types for you, provided that it does not involve a loss of precision. Since a long can hold all the values an int can, the cast will be performed automatically, but you would have to explicitly cast (for example) a long if you wanted to use it where an int should be.

edit: to cast a long "x" to an int, use:
``x = (int)x;``
Message was edited by:
Dross
• ###### 8. Re: comparing long to int
Currently Being Moderated
[nevermind, rubbish from me]

Message was edited by:
prometheuzz
• ###### 9. Re: comparing long to int
Currently Being Moderated
Thanks I will test now. Also can you please tell me
how I convert a Long to an Integer?

Thanks

vik
``````int a ;
long b = 1;
a=Integer.parseInt(b+"");``````
• ###### 10. Re: comparing long to int
Currently Being Moderated
Thanks I will test now. Also can you please tell
me
how I convert a Long to an Integer?

Thanks

vik
int a ;
long b = 1;
a=Integer.parseInt(b+"");
@vik: be carefull when doing the thing ggopi suggested, when b exceeds 2147483647 (== (2^31)-1 == Integer.MAX_VALUE), you will get a NumberFormatException.
• ###### 11. Re: comparing long to int
Currently Being Moderated
(Earlier, I posted:
``x = (int)x;``
)

I'm very sorry, that was incredibly sloppy of me!

That should be:
``````int x;
long y = 0;
x = (int)y;``````
Alternatively, you can just use (int)y as if it were an integer.

Message was edited by:
Dross