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?
jeffrey z. lee
Nazmul Huda Sarkar
Ranch Hand
Joined: Feb 01, 2002
Posts: 317
posted
0
The code should work fine...as the string will be auto converted to int....
Nazmul<br />SCJP,SCWCD,IBM OOAD with UML
Howard Kung
Greenhorn
Joined: Jul 22, 2002
Posts: 24
posted
0
According to http://www.withmilk.com/ (where the question came from), the answer should be B, C. Any comment? anyone?
Howard Kung<br />SCJP 1.2<br />SCWCD<br />IBM Certified Specialist: WebSphere 4.0
Raj Paul
Ranch Hand
Joined: Jul 09, 2002
Posts: 77
posted
0
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
Joined: Jul 08, 2000
Posts: 1006
posted
0
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!
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
Joined: Oct 27, 2002
Posts: 87
posted
0
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