Courtesy: my teammate, Ashok...


J S Ashok
04/27/2000 11:11 AM

To:   Anurag Panda/India/IBM@IBMIN
cc:   Premjith Manapetty/India/IBM@IBMIN, G Rajeev/India/IBM@IBMIN, Rupesh
      A Kartha/India/IBM@IBMIN
From: J S Ashok/India/IBM@IBMIN
Subject:  Re: puzzling  (Document link: Anurag Panda)

Hi Guys,
     I think u people must have figured out the reason. Looks very simple.

public static void main(String args[]){

     int a = 1;
     int b = 1;
     a = a++ * ++a;
     In this step first a++ so the operators are evaluated. so operand is
taken as 1. Now then a is incremented.
Then for the next operand a is first incremented, so the operands are 1 &
3. So now the operation is carried out.
a = 1 * 3 . Which puts the value of 3 to a.


     b = ++b * b++;

     In this step first is ++b so the operators are evaluated. so operand
is taken as 2. Now then b is not incremented before
the operation. so ++b and b are 2 operands which makes them 2 and 2. So the
 expression becomes b = 2 * 2;
so b is 4.

     System.out.println(a);
     System.out.println(b);
}


Ashok Jayaraman S
IBM Global Services,
Internet : [EMAIL PROTECTED]
IBMMAIL : J S Ashok/India/IBM
Direct No. : 91-80-526 2355 Extn. : 2501
Fax : 91-80-527 7374



Ramkumar Jeyaraman <[EMAIL PROTECTED]> on 04/27/2000 10:01:43 AM

Please respond to A mailing list about Java Server Pages specification and
      reference <[EMAIL PROTECTED]>

To:   [EMAIL PROTECTED]
cc:    (bcc: Anurag Panda/India/IBM)
Subject:  puzzling




Hi,

Sorry to post this silly doubt here, but I need
experts' answers, and I dont find experts anywhere
other than here.

Accoring to Core Java and other books :

1. Unary operators have higher precedence that
multiplication/division operators.

2. Associativity of unary operator is from right to
left.

In light of this, can anybody explain the output of
the following program :

public static void main(String args[]){

     int a = 1;
     int b = 1;
     a = a++ * ++a;
     b = ++b * b++;
     System.out.println(a);
     System.out.println(b);
}

I am unable to figure out the reason for the output.

regards,
ram



__________________________________________________
Do You Yahoo!?
Talk to your friends online and get email alerts with Yahoo! Messenger.
http://im.yahoo.com/

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to