Skip to content

Instantly share code, notes, and snippets.

@ptantiku
Created February 22, 2014 08:22
Show Gist options
  • Save ptantiku/9150450 to your computer and use it in GitHub Desktop.
Save ptantiku/9150450 to your computer and use it in GitHub Desktop.
Java integer overflow
class User{
public Integer money;
public User(Integer money){
this.money = money;
}
public Integer getMoney(){
return this.money;
}
public void setMoney(Integer money){
this.money = money;
}
}
public class A{
public static void main(String[] args) throws Exception{
// initial
Integer money = 100;
User user = new User(money);
System.out.println("BEFORE="+user.getMoney());
// set price
int price = Integer.MAX_VALUE/7*100+1;
System.out.println("PRICE="+price);
// validate price
if(price<0){
return;
}
// calculate vat
Integer vat_amount = (int)(price * 7 / 100);
System.out.println("VAT="+vat_amount);
// set User's money
user.setMoney( user.getMoney() - vat_amount);
System.out.println("AFTER="+user.getMoney());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment