What does it mean to put a type before a declaration? [duplicate]

1

Hello, I was studying HTTP Requests in Java when I came across a code and on a certain line there was a somewhat strange statement for me, can anyone explain to me what a type means before a statement?

 HttpURLConnection connection = null;
 URL url = new URL(targetURL);
 connection = (HttpURLConnection) url.openConnection(); //Esta declaração

Sorry for ignorance on the subject, but can anyone explain to me why HttpURLConnection type before the rest of the designation? Thank you!

    
asked by anonymous 08.01.2018 / 23:24

1 answer

2

The expression url.openConnection (); returns a URLConnection. Analyzing the API ( link ) we can verify that HttpURLConnection is a sub- class of URLConnection. For this reason it is possible to cast, so change the declaration from one type to another subtype, with the expression:

  

(HttpURLConnection) url.openConnection ();

    
09.01.2018 / 12:19