• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can someone explain these two syntax features

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private class WeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {


I got these two questions about the asynctask three types in one and the string ... parameters, I never saw these before. Can someone explain it to me. I was reading through java code and I don't know how to look these up. I think they are Generics but I never saw generics with three types. I also never saw java with a variable set of method parameters. I appreciate the help.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Willima,
Yes, the types inside the <> are generics. They allow you to say that a class works with certain types. You can have more than one generic type in a class. The class itself specifies how many there are. And the AsyncTask docs list three types.

The ... are varargs. From the point of the method, you use it like it is an array. You can call it with an array as well. The key difference is that you don't have to call it with an array. You can call it with 0 or 1 or 2 or 3 or ... Strings and Java will turn it into an array for you when it calls the method.
 
Ranch Hand
Posts: 606
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William H Taylor wrote:private class WeatherTask extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {


I got these two questions about the asynctask three types in one and the string ... parameters, I never saw these before. Can someone explain it to me. I was reading through java code and I don't know how to look these up. I think they are Generics but I never saw generics with three types. I also never saw java with a variable set of method parameters. I appreciate the help.



It is really easy. Under Android the Google Developers preferred to manage background services with more performing tools, so they built AsyncTask as an "helper" for short operations.
You have to look up 2 things the parameters inside the diamond brackets and the methods. I can recognize your code is the one released by google for their udacity course.
It is quite complex, because it is connected to JSON backend, I would suggest you to type yourself a simpler example, focusing on the first parameter called Params that will be passed to the method doInBackground(Params...) and you can leave the other two parameters Void ex <String, Void, Void>. Just remember that you need to call the AsyncTask instance to activate AsyncTask.

 
William H Taylor
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand, thank you very much.
 
Greenhorn
Posts: 11
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also important to note, that if you use varargs, they must be the last parameter in a method. So for example, the following method uses var args, and other parameters. The compiler will complain i you try to use the varargs parameter before the end.


 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic