Created
February 22, 2014 08:22
-
-
Save ptantiku/9150450 to your computer and use it in GitHub Desktop.
Java integer overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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