• 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

int Data type

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

As we all know int range is (–2,147,483,648 to 2,147,483,647).

below is the code

int a=2147483647; //1

int mul=a*2; //2
int add= a+2; //3

System.out.println(mul);
System.out.println(add);

out put is

-2
-2147483647

The question is when declaring int if we declare more thani nt range it gives complie time error...
but when we are using the same variable which is in range and we add or multiply it as in above
at runtime is give some junk values


how do we know at runtime int value will be gretae then its range .
do we need to explicitky check by using if condition ...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how do we know at runtime int value will be gretae then its range .
do we need to explicitky check by using if condition ...




Unfortunately, Java doesn't throw exceptions for overflow and underflow conditions. It just wraps around. So, the easiest way to tell that it happened, is if the sign changed (when it obviously should not have).

Henry
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic