Valid and Invalid Expressions
<%= 27 %> :
VALID
All primitive literals are fine.
<%= ((Math.random() + 5)*2); %>
INVALID
NO! The semicolon can’t be there at end
<%= “27” %>
VALID
String literal is fine.
<%= Math.random() %>
VALID
Yes, the method returns a double.
<%= String s = “foo” %>
INVALID
NO! You can’t have a variable declaration here.
<%= new String[3] %>
VALID
Yes, because the new String array is an object, and ANY
object can be sent to a println() statement.
<% = 42*20 %>
INVALID
NO! The arithmetic is fine, but there’s a space between
the % and the =. It can’t be <% =, it must be <%= .
<%= 5 > 3 %>
VALID
Sure, this resolves to a boolean, so it prints ‘true’.
<%= false %>
VALID
We already said primitive literals are fine.
<%= new Counter() %>
VALID
No problem. This is just like the String[]... it prints
the result of the object’s toString() method.
VALID
All primitive literals are fine.
<%= ((Math.random() + 5)*2); %>
INVALID
NO! The semicolon can’t be there at end
<%= “27” %>
VALID
String literal is fine.
<%= Math.random() %>
VALID
Yes, the method returns a double.
<%= String s = “foo” %>
INVALID
NO! You can’t have a variable declaration here.
<%= new String[3] %>
VALID
Yes, because the new String array is an object, and ANY
object can be sent to a println() statement.
<% = 42*20 %>
INVALID
NO! The arithmetic is fine, but there’s a space between
the % and the =. It can’t be <% =, it must be <%= .
<%= 5 > 3 %>
VALID
Sure, this resolves to a boolean, so it prints ‘true’.
<%= false %>
VALID
We already said primitive literals are fine.
<%= new Counter() %>
VALID
No problem. This is just like the String[]... it prints
the result of the object’s toString() method.