title: In Java is there a preferred method of casting an object?
body: <p>I have a superclass person with a subclass student. I have made a student object of type person, and wanted to call a student specific method which does not exist in the person type. I have two methods below that can do this, and I was curious to know is one method superior to the other, or do they both achieve the same outcome?</p>
  
  <pre><code>    Person newStudent = new Student("Scott", 22, "B22334952");
  
      //method1: make a reference of newStudent and cast to Student
      Student studentRef = (Student) newStudent;
      System.out.println(studentRef.getUserId());
  
      //method2: cast newStudent, but don't create a reference
      System.out.println(((Student) student2).getUserId());
  </code></pre>