Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

operators

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Q9{

public static void main(String[] args){

int i=3;

System.out.println(i*=2+i++);

}

}
please explain the way output is coming .
output is 15.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
somebody please come up with something
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
int i= 3;
i*=2+i++;
we can put it as i=i*(2+i++)
then first evaluate and then asign
i=3*5
i=15
if we did
i*=2+(++i);
then i would be 18

[This message has been edited by Mafalda Alabort (edited March 24, 2001).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here the expression solved from right to left order
step 1
it solves the i++ which gives result as 3 i.e) the value is taken before it gets incremented
step2
the 3 is added with 2 gives 5
step 3
the value 5 is multiplied with 3 (value of i) due to *= , which gives answer as 15

Originally posted by nitin sharma:
public class Q9{

public static void main(String[] args){

int i=3;

System.out.println(i*=2+i++);

}

}
please explain the way output is coming .
output is 15.


 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's expert's
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have answered for the same question here.
If interested please check the following link http://www.javaranch.com/ubb/Forum24/HTML/008873.html
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mafalda Alabort:
Hi Nitin,
int i= 3;
i*=2+i++;
we can put it as i=i*(2+i++)
then first evaluate and then asign
i=3*5
i=15
if we did
i*=2+(++i);
then i would be 18

[This message has been edited by Mafalda Alabort (edited March 24, 2001).]


Hi Mafalda ,
I think wht. u have written last is wrong.
"if we did
i*=2+(++i);
then i would be 18"
I think it should be 24.

Correct me if I am wrong.
<marquee>Ratul Banerjee</marquee>
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the expression:
i*=2+(++i);
would indeed be 18 and not 24. ++i would give you 4. Then you would add that to 2. and then multiply by 3 giving you 18.
 
tvs sundaram
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel, What ameen ahamed written is not correct
The order is not from right to left but the other way.
The problem:
In the above code i want's to know when System.out.println(i*=2+i++); is executed first of all i++ will get executed then the value if i(3)will be added to 2 and the result is 5 ,now my question is when the next i=i*5 is executed the value of i should have been 4 because of post increment value which multiplied by 5 should give 20 not 15.
Please throw some mashal(light) on it.
My answer
As per RHE book, page 33, the evaluation order in an expression is fixed
i.e from left to right.
Now let us try to solve your problem.
If x*=a; then this should be interpreted as x=(T)(x*(a));
By following this rule in your question the steps are as follows.
System.out.println(i*=2+i++);
System.out.println(i=(T)(i*(2+i++));
Now u assign values from left to right once the values are known
System.out.println(i=(T)(3*(2+3++));
System.out.println(i=(T)(3*(2+3)); // Note i is post increment operator.
So the answer printed will be 15.
I repeat:
Also note the braces are very clear as per rules and precedence play no role in this problem.
Hope this helps.
comments pls...

Hope this makes ur problem clear.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!