This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators & Assignments Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Operators & Assignments" Watch "Operators & Assignments" New topic
Author

Operators & Assignments

Abi Raj
Greenhorn

Joined: Jan 28, 2005
Posts: 28
I have some doubts in Operators & Assignments. pls clarify them

1) Is (--1) a valid expression? If not why.
2) Is (+-+-+-1) a valid expression??
3) what will be the output of the following program

public class Evaluationorder{
public static void main(String[] args) {
int[] array = {4,8,16};
int i = 1;
array[++i] = --i;
System.out.println(array[0]+array[1]+array[2]);
}
}

when I run the program it prints 13. Can somebody explain it??
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411


At Line X
First i gets incremented to 2, we have array[2] = --i; and then i gets decremented by 1 so i = 1. So finally array[2] = 1;

So when you add array[0] = 4 , array[1] = 8 and array[2] = 1 you get output as 13.

Hope that helps you...


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Operators & Assignments
 
Similar Threads
bitwise operator
Help: Oeperator Associativity 2?
Array
Precedence/Associativity
Confuse