java - What are these things in my method signature called? Generics? -


I have a method sign that shows the map given by prices.

  Public stable and lieutenant; Comparative in Kashmir & lt; K & gt; Increases, V comparative & lt; V> & Gt; Click the & lt; Kashmir, V & gt; Various types of values ​​(map & lt; K, V & gt; map) {  

I am trying to understand the vocabulary for things in the signature of this method. I can see public , static , map & lt; K, V & gt; , the function name and parameter are found.

My confusion & lt; Comparative on Kashmir and lieutenant; K & gt; Increases, in V comparative & lt; V> & Gt; The section is expanded, its purpose is to determine that the of and V are in the context of the method, right?

Yes, K And V are general here to be more specific, as in the @Ryusudanet comment, they are bound type parameters because they consider a specified parameter to be sorted Must complete - that is, they should implement both comparatively

It seems that your question is about the parameter, however, Not specifically bound parameters, you can definitely do some of your own reading, as you are asking, but I take a stab when I start it:

You can type parameters in different contexts You can get acquainted by seeing - Maps & lt; String, integer & gt; : & lt; String, integer & gt; There are also type parameters, and they specify that the keys to be done in this map are string S and the values ​​are integer s reference to a method declaration In, the type parameters specify what the return type will be. This means that you can type different types of returns in different ways according to whatever you type:

Note that K and V There are type parameters on the parameters passed in both the method and the parameter: It tells Java that in view of the main and value types of the map to determine K and V , one Return a map with the same type. If you call sortByValue (fooMap) and fooMap a map & lt; String, FooType & gt; , then this is a map & lt; String, FooType & gt; , and if fooMap is a map & lt; Integer, bartype & gt; , you will see a map & lt; Integer, bartype & gt; back

For a clear example, consider:

  public < T & gt; FOBR (TT) {}   

The type of parameter here is infinite - which means that T can be any class, so that you can move the object of any square You can pass as a parameter and get back the same type of object. Or

  public & lt; T & gt; Convert (object o, class and lieutenant; t> clause) {}   

Here, you can pass in the object of any class as the first parameter, and the second As a parameter, the Class object determines what happens if T is what you can do:

  Convert Focalus s = (Some Objects, Focalus. Classes)   

Which you might imagine, it can be very useful.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -