Avatar of zizi21
zizi21

asked on 

Java : Urgent - Unary

Hi,
Sorry for the english. Sitting for exam so a bit panic. pls help...

I have a program like below:

public static void main(String [] args){

int x=3;
int y;

y=x++ % ++x;

System.out.println(y);
System.out.println(x);
}
}

When i ran y is 3 and x is 5. I read from book, it says that unary operators are evaluated from right to left..
So, if this is the case, ++x is evaluated first ( x=4) , then , x++, so, x=4, y=0 and not 3...pls clarify
Java

Avatar of undefined
Last Comment
zizi21
Avatar of Mick Barry
Mick Barry
Flag of Australia image

% is not a unary operator
Avatar of Mick Barry
Mick Barry
Flag of Australia image

y = 3 % 5;  // = 3
Avatar of Mick Barry
Mick Barry
Flag of Australia image

ie. they are evaluated left to right
Avatar of zizi21
zizi21

ASKER

but x++ is unary
Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes x++ is evaluated first -> 3
then ++x -> 5
Avatar of zizi21
zizi21

ASKER

or..do you mean in precedence, postfix is done before prefix?thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

no, when evaluating "a % b", a is evaluated before b
Avatar of zizi21
zizi21

ASKER

i was reading the text book, it says for precedence...


postfix  right to left
prefix,unary, right to left
binary operators left to right..

this is so confusning :-(
Avatar of zizi21
zizi21

ASKER

what do you do they mean right to left then ?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

yes that is correct, but the binary operator gets evaluated before the unary operators do
Avatar of Mick Barry
Mick Barry
Flag of Australia image

so yes the unary operators are evaluated right to left,
and the % is evaluated left to right, thus evaluating x++ first
Avatar of zizi21
zizi21

ASKER

if its like this..


y=++a % b++;

can i say that b gets evaluated, then a gets evaluated...and lastly for % , we do a%b ??
sorry, i am still confused...pls help
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> y=++a % b++;

1. ++a is evaluated
2. b++ is evaluated
3. (result of 1.) % (result of 2.) is evaluated
Avatar of zizi21
zizi21

ASKER

got it , but why do they say , that for postfix has higher precedence, does it mean, that if i have postfix, then i evaluate right to left...

for example

++a ++b ++c

1) c is evaluated first
2) b is evaluated second
3) a is evaluated third
Avatar of Mick Barry
Mick Barry
Flag of Australia image

right to left relates to how the unary operator is evaluated, it doesn't affect how the rest of the expression is evaluated.
Avatar of zizi21
zizi21

ASKER

sorry,,..how does unary operator gets evaluated then ?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

so in your case the unary ops get evaluated first because they have higer precedence.
then the % gets evaluated
Avatar of zizi21
zizi21

ASKER

hi, i understand : y=x++ % ++x;

but it says that postfix has higher precedence...
Avatar of zizi21
zizi21

ASKER

Operators      Precedence
postfix      expr++ expr--
unary      ++expr --expr +expr -expr ~ !
multiplicative      * / %
additive      + -
shift      << >> >>>
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of zizi21
zizi21

ASKER

got it, thank you very much..
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo