What does it mean to put a guy before a statement?

Asked

Viewed 92 times

1

Hello, I was studying HTTP Requests in Java when I came across a code and in a certain line there was a statement a little bit strange to me, someone can 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

Excuse the ignorance on the subject, but can someone explain to me why of type Httpurlconnection before the rest of the designation? Thanks in advance!

  • 2

    This is called a cast, it is used to convert a value type to another type, in which case it is converting the URL type into Httpurlconnection

  • Thanks friend, I do not like very much to copy and paste without understanding the code! You helped me too much! Hug.

1 answer

2


The expression url.openConnection(); returns a Urlconnection. Analyzing the API (https://docs.oracle.com/javase/7/docs/api/java/net/URLConnection.html) 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 type, with the expression:

(Httpurlconnection) url.openConnection();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.