• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Can someone explain these two syntax features

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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: 40200
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • 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: 528
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I understand, thank you very much.
 
Greenhorn
Posts: 11
Android Spring Java
  • Mark post as helpful
  • send pies
  • 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.


 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic