aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need help on precedence of operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need help on precedence of operators" Watch "need help on precedence of operators" New topic
Author

need help on precedence of operators

betzi kuriakose
Greenhorn

Joined: Jun 26, 2005
Posts: 12

the above code will
show the value of i as 0
and the array elements will be displayed as
3
0
0
0
0

Please explain why the output is so? I couldnt figure out...

regards
betzi
[ July 03, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"betzi", your displayed name is still not correct. Please use two names in one of the two formats <first name><space><family name> or <initials of first name><space><family name>.

Thanks
-Barry
[ July 03, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
The output is correct.
In the first expression i=i++ the value of i is substituted on RHS as it is i.e i=0.Now since the ++ operator has higher precedence than assignment operator(=).i is incremented by 1.So i becomes 1.But Note:Although i is incremented by 1 the value substituted on RHS was 0.Now in the end, the assignment operator is applied and this 0 is assigned to i.Same thing happens in all the three i=i++ expressions.

Thus the program prints i as 0 after those expressions.

int index = 0;
array[index]=index=3

In the above expression [] operator has higher precedence than '=' operator.Therefore its evaluated as below:

array[0] = index = 3
array[0] = (index = 3) //assignment operator is right associative
array[0] = 3

Since it is an integer array..all other uninitialized elements of array will take the default value for int i.e 0
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
1. Great name change, betzi!
2. Great answer, janki!
[ July 03, 2005: Message edited by: Barry Gaunt ]
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
take a look at the following code snipet does the exact opposite of the earlier code

public class IncrementTest
{
public static void main(String[] args)
{
int i = 0 ;
i = ++i;
System.out.println(" The value of i is " +i );
i = ++i ;
System.out.println(" The value of i is " +i );
i = ++i ;
System.out.println(" The value of i is " +i );
}
}

o/p is :
---------- java ----------
The value of i is 1
The value of i is 2
The value of i is 3

thanks and regards ,
bhavesh
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Please janki tangeda, what did u mean by RHS?
thank you.


SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Right Hand Side
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need help on precedence of operators