The [] gives you more options...

When you use the dot operator, the thing on the left can be only a Map or a bean, and the thing on the right must follow Java naming rules for identifiers. But with the [ ], the thing on the left can also be a List or an array (of any type). That also means the thing on the right can be a number, or anything that resolves to a number, or an identifier that doesn’t fit the Java naming rules. For example, you might have a Map key that’s a String with dots in the name (“com.foo.trouble”).

  1. If the expression has a variable followed by a bracket [ ], the left-hand variable can be a Map, a bean, a List, or an array.

    image with no caption
  2. If the thing inside the brackets is a String literal (i.e., in quotes), it can be a Map key or a bean property, or an index into a List or array.

    image with no caption

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.