title: Type Casting In Groovy
body: <p>I have two questions.</p>
  
  <p>I did the following code to find the ASCII value of <code>$</code> : </p>
  
  <pre><code>def a = "\$"
  def b = (int)a 
  println b //prints 36
  </code></pre>
  
  <p>Well I'm happy with the answer. But when I tried to do it in reverse like this, I found I'm    missing something :</p>
  
  <pre><code>def a = 36
  String b = a
  println b // getting output only 36 
  </code></pre>
  
  <h3>Question 1:</h3>
  
  <p>So my first question is why it prints <code>36</code> and why not <code>$</code>? Am I wrong here?</p>
  
  <p>Well if the same first code block is re-written as: </p>
  
  <pre><code>    def a = "\$"
      def b = a as int
      println b
  </code></pre>
  
  <p>If I run this program I get an error like this : </p>
  
  <pre><code>Caught: java.lang.NumberFormatException: For input string: "$"
      at T.run(T.groovy:2)
  </code></pre>
  
  <p>Even though I'm trying to do the same as before. I'm getting an error. </p>
  
  <h3>Question 2:</h3>
  
  <p>So why does the <code>as</code> keyword doesn't work here and does <code>def a = (int)b</code> is <strong>not equal to</strong> to <code>def a = b as int</code>? Explain me.</p>
  
  <p>Thanks in advance.</p>