• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on usage of dot operator in EL

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it true that anywhere the .(dot) operator is used, the [] could be used instead?
With regards,
Padma priya N.G.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
expr-a.identifier-b is equivalent to expr-a["identifier-b"]
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you please elaborate this?
With regards,
Padma priya N.G.
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,

You can use . operator instead of [] only for Maps and beans. Not for arrays and lists.

For arrays and lists, you have only [] operator.

See, array and list are indexed by integers.
And for them writing
${arrayName.1} will give you an error. Because 1 is not a valid variable name as per Java convention (Any variable name can't be started with number)

However writing ${arrayName[1]} or ${arrayName["1"]} or ${arrayName['1']} will solve the purpose.

One of the main benefit of EL is that , you can have those identifiers as a variable name which are not permitted by Java standards.

And please keep in mind
You can use . operator instead of [] only for Maps and beans. Not for arrays and lists.

Regards,
Khushhal
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the original question was

------------------------
Is it true that anywhere the .(dot) operator is used, the [] could be used instead?
------------------------

and the answer is "yes"

The reverse is not true for reasons that have been discussed previously

I assume you have asked because this is a typical question asked on mock exams
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks.
With regards,
Padma priya N.G.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic