This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL Qz Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL Qz" Watch "EL Qz" New topic
Author

EL Qz

liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
Hi all,

What will happen when you attempt to compile and run the following JSP page contents?

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.


DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Please refer page 399 of HFSF(Head First Servlets & JSP), 2nd Edition.

Thanks & regards,

Naveen Katoch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I can't understand it.

What exactly don't you understand ? 1 ? 2 ? 3 ?


[My Blog]
All roads lead to JavaRanch
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
Originally posted by liqiang yang:
Hi all,

What will happen when you attempt to compile and run the following JSP page contents?

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.


$(One + 1) : evalauates to 0 + 1 = 1 ( Attribute with name "One" could not be found in any of the scope, so EL consider it as 0 in arithmetic operation) (considered as false in logical operation)

${Two) : it is simpe. attribute names "Two" is found in scope and its value is 2.

${Two + 1} : Simple.Value of attribute "Two" (2) + 1 = 3

so it prints: 123 (There are no line breaks)

Regards

Prem Kashyap
[ May 14, 2008: Message edited by: Prem Kashyap ]
Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 574
    
    1

Originally posted by liqiang yang:

<%
request.setAttribute("Two","2");
Integer One = new Integer(1);
%>

${One + 1}
${Two}
${Two + 1}

The answer is: 123
I can't understand it. Can anybody figure it out? Thanks alot.


Well,
1)One is evaluated to null so null + 1 is 1 (I am not sure ,sorry)
2)Two is String and evaluated to primitive (if you put "rft" instead of
"2" you will get big fat exception)
3)Two("2") is evaluated to 2 and 2+1 = 3

As to 'One',I am not sure so other ranchers will take care of it

best regards,
omi
[ May 14, 2008: Message edited by: omi sharma ]

Back to Java , again.
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
As I said in my previou post, null is evaluated as 0 in arithmetic operation (+,- etc) and evaluated as false in logical operation(&&,|| etc)). (For relational operation, like <, >, >= etc , i am not sure if it evaluated to 0 or false. my guess it evaluated to false. Just write a small program and find out.

Regards
Prem Kashyap
liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
Thanks ranchers!!!

I make sense and feel better now.
 
wood burning stoves
 
subject: EL Qz
 
Similar Threads
LinkedHashMap program
doubt marcus mock exam question
EL question
Object creation for Integer class
Objects and References....Constructors and new