What does it mean (button)?

I am starting in Java and came across this line of code:

Button orderButton = (Button)findViewById(R.id.order);

What does (Button)it mean when it is inside the bracket?

What is the term for it inside?

+3
source share
4 answers

This is a type. You produce the result for typeof (Button)

+6
source

The purpose of casting is to let the runtime and IDE know what type of object is returned by findViewById (R.id.order);

findViewById (R.id.order) does not return a specific type, but a generic object. Since Button contains methods related to the element referenced by the object, you need to specify its type so that the new orderButton variable has access to the correct methods.

This is a simple example.

http://www.java-samples.com/showtutorial.php?tutorialid=1170

+3

this is typecast . findViewByIdreturns an object View, but you need an object Button. (Button)displays a view in a button

+1
source

This is called type casting . The object returned findViewById(R.id.order);is most likely from it Button. The person who wrote the code believes that the object returned by this function call is actually an instance Button, so he enters the type of the return value of the function into the instance Button.

+1
source

All Articles