Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem wrapper value

 
Prasun Howlader
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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?
 
sweta doshi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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?
 
Narendra shah
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic