This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
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

Khalid M Operator and Assigment question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try to compile and run the following program?
public class Prog1{
public static void main(String args[])
{
int k=1;
int i=++k + k++ + +k;
System.out.println(i);
}
}
Why is the answer ((++k)+ (k++))+(+k)->((2)+(2))+(3))-->7
When I tried using Maha Anna's technique
(((2)1+1(2))+(2))->4
I am not sure if I am using it right.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting 7.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this would be my first post for reply so bear with me for my language..
Answer for you would be that it has to do with the way prefix and postfix operators are executed.
in prefix : the current value is incremented / decremented before its assigned
in postfix : the current value is assigned first then incremented / decremented.
for ex: int i = 0;
int j = 0;
i++;
++j;
System.out.println(i); // will print 0
System.out.println(j); // will print 1
hope it helps.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well barkat thats what the question is. Anyone will get 7 on running that piece of code and the reason is easily understood by operator precedence. So why did siva expect 4??
Siva can you please let me know what Maha Anna's technique is (the link will help). Maybe the technique is correct and you interpreted it in a wrong way.
 
Siva Sivaraman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
Here is the link for Maha Anna's techique...
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Siva Sivaraman
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it right this time.
The correct way of applying the technique is:
(((2)2+2(3))+3)-->2+2+3-->7
Note:Substitute the latest value.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int k = 1;
int i=++k + k++ + +k;
Read the link - very interesting. Going by that technique:
The calculation is as follows:
i = ++k + k++ + +k;
= 2(2) + 2(3) + 3
= 2 + 2 + 3
= 7
when you pre-increment, the value of k remains the samei.e, the incremented value is used in the calculation. It's only when you post-increment, that you assign or use the current value in the calculation and then increment it - hence the
2(3) in the calculation above.
That's my interpretation of the technique. Correct me if I am wrong.
Sharda
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite a technique but seems like my old rusted brain cannot digest this..
do u know any other techniques.. i mean i know this ++ , -- from my college days so i guess i can do it but do you guys have some more techniques for other things.. do share with me...
Thanks
Bishal
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!