title: how to use asInstanceOf properly in Scala
body: <p>I have been playing with basic Scala data types. I noticed that the <code>scala.Any</code> class defines the method <code>asInstanceOf[T0]: T0</code> from <a href="http://www.scala-lang.org/api/current/index.html#scala.Any" rel="noreferrer">here</a> The API has it that it can "Cast the receiver object to be of type T0". Using this method as a starting point, I wanted to investigate casting in Scala. Also, I looked up stackoverflow for other questions on this topic and I came up with <a href="https://stackoverflow.com/questions/3412068/what-are-the-differences-between-asinstanceoft-and-o-t-in-scala">this</a>
  With this information in hand, I wrote up a silly program. </p>
  
  <pre><code>         package com.att.scala
           import com.att.scala.Sheltie
  
           object Casting {
  
               //def foo(x: String){ 
               def foo(x: Int) {
                println("x is " + x)
               //if(x.isInstanceOf[String])
                if(x.isInstanceOf[Int])
                   println("Int x is " + x)
                  //println("String x is " + x)
               }
  
              def entry() {
                 //val double: Any = 123.123
                 val double: Double = 123.23
                 val int = double.asInstanceOf[Int] //exception expected here
                 //val str: String = "123"
                 foo(int) 
               }
  
           }
  </code></pre>
  
  <hr>
  
  <p>My goal is to understand what happens (and why) in the following cases:
   1) casting from an Any type to an Int. 
    2) casting from Double type to an Int 
    3) casting from String to Int</p>
  
  <ol>
  <li><p>In the first case, I got a run time ClasscastException as below, when I ran the program as - com.att.scala.Casting.entry. The exception is shown below:</p>
  
  <p><code>java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
  at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
  at com.att.scala.Casting$.entry(Casting.scala:17)</code></p></li>
  <li><p>In the second case, I get the following result:
  int is 123
  x is 123
  Int x is 123</p></li>
  </ol>
  
  <p>In this case, the code is supposed to produce a ClasscastException, but it does not. That is my worry.</p>
  
  <ol start="3">
  <li>In the third case, I get a classcastexception:</li>
  </ol>
  
  <p><code>java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
      at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
      at com.att.scala.Casting$.entry(Casting.scala:20)</code></p>
  
  <p>With this example,my goal is to get to the very basics of casting in Scala. I know that this example is not a real world example by any means, but I was trying to get my head wrapped around the basics.</p>