0

i have a short question. For a very long time i implements ArrayList like this:

public ArrayList<Double> random = new ArrayList<Double>();

And no one correct me, so i thought it is good, but android studio underlines "Double" in 2nd ArrayList, but it works anyway

public ArrayList<Double> random = new ArrayList<>();

now Android studio do not show error, so what is the difference ?

Surya Prakash Kushawah
  • 3,185
  • 1
  • 22
  • 42
lukash
  • 3
  • 4
  • Type is inferred from the declaration. That should be obvious. It doesn't work for static variables, though. You should consider using List on the left hand side for the compile time type. – duffymo Dec 23 '16 at 17:52
  • 3
    after java 7 there is no need to specify the type when you instantiate the object – A Sdi Dec 23 '16 at 17:52
  • 3
    See http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 . And you can also specify just a `List` on the left side: `public List random = new ArrayList<>();` – ROMANIA_engineer Dec 23 '16 at 17:53
  • "For a very long time", you probably started learning about generics around Java 6, then https://stackoverflow.com/questions/17357883/using-generics-on-right-hand-side-in-java-6 – OneCricketeer Dec 23 '16 at 18:01
  • Both are correct, but the first is redundant, since you have already specified the type. – Bethany Louise Dec 23 '16 at 19:12

1 Answers1

1

It should be no problem by Java standards. Android prefers what Oracle calls a diamond operator for type inference.

You may read more about it here: http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html There is also a long post about it on Stackoverflow: What is the point of the diamond operator in Java 7?

Community
  • 1
  • 1
Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67