Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question

 
jeffrey z. lee
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that file1.jsp has the following custom tag:
<m:test times="5"/>
And the actual class file has the following setter method for variable 'times':
public void setTimes(int t){
this.t=t;}
Identify true statements:
A Code works fine
B Parameter t should be String int the method : setTimes(String t)
C The body of the method should be: this.t=Integer.parseInt(t)
D The name of the variable should be 'times'

is ans. C true? why?
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code should work fine...as the string will be auto converted to int....
 
Howard Kung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to http://www.withmilk.com/ (where the question came from), the answer should be B, C. Any comment? anyone?
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the answer should be B,C
Because, in taglibs the automatic conversion is not there like java beans. In taglib we have a tag named <type> which specifies the type of the variable. default is String.
so B&C is correct

Raj Paul
 
Paul McKenna
Ugly Redneck
Ranch Hand
Posts: 1006
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strictly speaking B is the correct answer, why?
1. All values returned at page translation time are of the type String. Hence only B would result in correct compilation
2. C would not be an appropriate answer because assuming the method signature does not change, just an Integer.parseInt would not accomplish anything
3. Automatic type cast is not performed in the case of Tab library attributes. Automatic type cast is only allowed when the rtexprvalue tag is set to true in the TLD file.
Zeig Hail!
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Provided that the attribute doesn't accept request-time expressions (rtexprvalue is false), automatic type translation will occur and the code will work fine.
Remember, automatic type translation doesn't take place when rtexprvalue is set to true.
As an aside, if the signature is changed (answer B), then the body of the method should be changed (as per C), provided that the instance variable is still declared as a primitive int, of course.
HTH
Simon
 
Maha Annadurai
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the given example uses a literal value for the "times" atrribute like times="5" , even if we set <rtexprvalue>true</rtexprvalue> in tld file, the code will work fine.
Literal values are always auto-converted to java standard objct types like Integer /Byte /Boolean etc.
For the above example, with rtexprvalue set to true, here are some tests you can play with.
--------------------------------
<test:body count="1"> (will work)
Example 1
</test:body>
-----------------------------------
<test:body count="<%=2%>"> (will work)
Example 2
</test:body>
-----------------------------------
<test:body count='<%="2"%>'> (will not work)
Example 3
</test:body>
-----------------------------------
<test:body count='<%=Integer.valueOf("2").intValue()%>'> (will work)
Example 4
</test:body>
-----------------------------------
Regards,
Maha Anna
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic