-13

My question may be really simple but I just want to know when you would use blank parameters.

Example:

ArrayList<String> list= new ArrayList<String>(); 

See at the end of the code there are blank parameters. In which situations would you use those blank parameters?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sameer54321
  • 325
  • 2
  • 8
  • Get a good Java book Sameer. That should answer this :) – Karan Ashar Jun 16 '15 at 19:27
  • @sameer54321 This may be help you [Creating Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html) – Bacteria Jun 16 '15 at 19:30
  • You mean *no arg constructor* rather than *empty parameters*. You should read the documentation of the class and decide if you're initializing it with the default values or if you want/need to provide specific parameters. It depends if the API allows you to do that, of course. – Luiggi Mendoza Jun 16 '15 at 19:30
  • @LuiggiMendoza: lack of code formatting hid the type parameters. Indeed not a duplicate in that scenario. – Jeroen Vannevel Jun 16 '15 at 19:32

1 Answers1

7

You'd use them when you don't need to give the constructor or method any data. In the case of your ArrayList it's to make an empty array with a default capacity of 10 elements (OpenJDK implementation).

As an abstract example, I can tell you to eat() but I don't care what you eat, so I don't tell you what to eat. (in the case of a method)

Or I can make a new Bookshelf() but I don't have any books, so I don't tell it anything, I just need an empty bookshelf. (in the case of a no-args constructor)

DeadChex
  • 4,379
  • 1
  • 27
  • 34