title: How does casting long to integer work?
body: <p>I'm writing some code where I have to cast a long to an int. The input comes in as a string and the actual number is too large to immediately parse as an int. So I tried parsing as a long and then casting to an int, which worked and confused me because I though it wouldn't.</p>
  
  <pre><code>public void setNum(String Num) {
     if (Num != null){
         //THIS MAKES NO SENSE **to me** :(
          this.Num = (int) Long.parseLong(Num); 
     } else {
          this.Num = -1;
     }
  }
  </code></pre>
  
  <p>I've read up on a couple other topics that I've found on here, notably <a href="https://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java">this one</a> and while it goes over how to safely convert them, it doesn't explain how the process works and I'm curious as to how it does. I know that in cases where the long is > Integer.MAX_VALUE will result in data being changed when it's casted, I'm still just confused as to how the casting actually works.</p>