File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators & Assignments 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 "Operators & Assignments" Watch "Operators & Assignments" New topic

Operators & Assignments

Abi Raj

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;

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:
subject: Operators & Assignments
It's not a secret anymore!