• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP doubt?

 
Joe Harry
Ranch Hand
Posts: 10113
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

The code below is from David Bridgewater's,


The question is What is output to the web page on the second access to the same instance of the following JSP?
(Choose one.)

How the answer is 3?? I thought 4!
 
Saurabh Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when this jsp is accessed first time, it will run JspInit() thus incrementing x by 1.Then, <%= x++ %> would increment x by 1.
By now x=2.
Now if you access the same instance 2nd time, JspInit() wouldnot be called. just <%= x++ %> would increment x by 1.
So the final value of x will be 3.

Thanks and regards,
Saurabh
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By now x=2.
Now if you access the same instance 2nd time, JspInit() would not be called. just <%= x++ %> would increment x by 1.
So the final value of x will be 3.[/QB]


Shouldn't it output 2 instead of 3?

<%= x++ %> ---> out.print(x++); // print x, then increment x
But:
<%= ++x %> ---> out.print(++x); // increment x, then print x


[Edit]
Sorry. It should output 3. I forgot to take into account the

at the end of the jsp.
[ April 13, 2007: Message edited by: Sergio Tridente ]
 
Joe Harry
Ranch Hand
Posts: 10113
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic