title: Sum and subtract chars in Java and always get result within the ASCII range
body: <p>I have created two functions that sum and subtract the numeric values of two chars and return the char of the result in PHP. It's something like this:</p>
  
  <pre><code>function sumchars($c1, $c2) {
      return chr(ord($c1) + ord($c2));
  }
  
  function subchars($c1, $c2) {
      return chr(ord($c1) - ord($c2));
  }
  </code></pre>
  
  <p>Well, I also created these two function in Java and they are supposed to work as the PHP ones.</p>
  
  <pre><code>public static String sumchars(char c1, char c2) {
      return (char)((int)c1 + int(c2));
  }
  
  public static String subchars(char c1, char c2) {
      return (char)((int)c1 -+ int(c2));
  }
  </code></pre>
  
  <p>It seems like the PHP function chr() knows how to treat results that are outside the ASCII range (greater than 255) and also those that are negative (less than 0). Not the same happens in Java (where instead of a function I just cast the integer result). I know that I could create a simple function for that but I wonder if there's already one. I want these functions to be very fast and I would prefer to use something already implemented rather than a function created by me.</p>
  
  <p>Thanks in advance for your recommendations!</p>