Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we always type cast in Android/Java?

Tags:

java

android

I am writing an Android app and just was curious about why we must always type-cast in Android. I understand that we need to be sure of the type so that our code runs properly, but is there, perhaps, another reason?

Example:

public class Navigation extends Activity {
    private DrawerLayout mDrawerLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // other irrelevant code
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

In the context of this question, I am asking why we must type-cast the returned value of the findViewById. (I'm also curious how this type casting is done if one could explain that.)

like image 687
nonamorando Avatar asked May 25 '15 01:05

nonamorando


People also ask

Why do we do type casting in Java?

Understanding Type Casting in Java Programmers need to check the compatibility of the data type they are assigning to another data type, in advance. Typecasting is automatically performed if compatibility between the two data types exists. This type of conversion is called automatic type conversion.

Why do we need to type cast?

We can use the process of type casting to convert the character (char) data type to the int (integer) data type in C. When we are performing a conversion between these two, the resultant value would be an integer (int) data type. It is because the int data type is comparatively bigger than the char data type in C.

Is casting automatic in Java?

In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion performed by the programmer.

What is type cast operator in Java?

The type cast operator converts the data type of expr to the type specified by type-name : [ type-name ] expr. Explicit type conversions require the type cast operator. Implicit type conversions are performed automatically by 4Test and do not require explicit type casting.


2 Answers

findViewById loads resources from a file. It returns some kind of View, but the Java compiler has no way of knowing that it will be a DrawerLayout (because it is defined in a file outside of Java's compilation).

If you need it to be a DrawerLayout in your code, you have to cast it. That makes sure (at runtime) that it really is such an object (if it is not, you will get a ClassCastException and your program will abort at that point).

The reason you want to cast it as DrawerLayout is that you may want to call methods on the instance that are defined in that class. Sometimes, you may be fine with just the signature of the more general superclass. Then you don't have to cast it.

To be sure, the instance returned by findViewById is a DrawerLayout no matter if you cast it or not. But if you don't cast it, then Java won't let you treat it as a DrawerLayout. That is the point of a type-safe language: Unless the compiler can be sure that an object is of a certain class, it won't let you call methods (or access fields) for that class.

like image 120
Thilo Avatar answered Oct 04 '22 02:10

Thilo


DrawerLayout is a subclass of View. A DrawerLayout object automatically has all the methods that a View has, but defines some methods of its own that are specific to DrawerLayouts. If you don't cast it, you can't use any of those DrawerLayout methods; you can only use the methods defined for all Views.

The type-casting doesn't do anything, really. findViewById is declared to return a View, since it can return all different kinds of Views. But in reality, it will often return an object of some other class, that is a subclass of View. The type-cast just checks to make sure that the returned object is really an instance of DrawerLayout (or one of its subclasses); if not, an exception is thrown, but if it is, you can now look at the object as a DrawerLayout and use the DrawerLayout methods.

like image 42
ajb Avatar answered Oct 04 '22 03:10

ajb