• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Problem wrapper value

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class DemoTest{
public static void main(String[]args){
int i = Integer.MAX_VALUE + 1;
System.out.println(i);
}
}
This program run successfully.Why no error generated?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

Look at the output ...it gives you " -2147483648 " which means it goes back to the Integer.MIN_VALUE . And hence does not throw any error .

Thanks
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could not understand my question?
compare with
int i = 2147483674 + 1
Now what happing?
 
sweta doshi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

You are trying to say that it should normally add 1 to the highest value which should result in an error simply because it is no longer in the range of int.. right ?

Thanks
 
sweta doshi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

int i= 2147483647 + 1;
If i use the above , it throws a compiler error saying that it is out of range since int cannot hold it . You already trying to stuff something into an int that is not appropriate for int. Rather use long . I hope this answers you .

Thanks
 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear sweta doshi you again tell the same thing.

Look at my first post. here i use wrapper Integer.MAX_VALUE +1 and assign the value into the int variable. But in that case it generated no error why?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dear. Which Compiler are you using. It do not throw any compiler error. I had tried with all 1.3,1.4,5,6. No errors in compilation or runtime.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasun as you mentioned in post you are trying to add 2147483674 and not 2147483647 to 1 and assign to i. Here 2147483674 is clearly out of range for int since its range is from -2147483648 to 2147483647
The below code compiles fine.

May you got confused with the MAx value.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic